0
0
Data Structures Theoryknowledge~6 mins

Directed vs undirected graphs in Data Structures Theory - Key Differences Explained

Choose your learning style9 modes available
Introduction
Imagine you want to show connections between places or people. Sometimes the connection goes both ways, and sometimes it only goes one way. Understanding these two types of connections helps us model many real-world problems.
Explanation
Undirected Graphs
In an undirected graph, connections between points (called nodes) have no direction. This means if node A is connected to node B, you can move from A to B and also from B to A freely. These graphs are useful when relationships are mutual, like friendships or roads where traffic flows both ways.
Undirected graphs represent two-way, mutual connections between nodes.
Directed Graphs
Directed graphs have connections that go one way only. Each connection has a direction, like an arrow from one node to another. This shows that you can move from the start node to the end node, but not necessarily back. Directed graphs model situations like one-way streets or follower relationships on social media.
Directed graphs represent one-way connections with a clear direction from one node to another.
Edges and Arrows
In undirected graphs, connections are called edges and are shown as simple lines between nodes. In directed graphs, connections are called arcs or directed edges and are shown as arrows pointing from one node to another. This visual difference helps quickly understand the type of relationship.
Edges in undirected graphs are lines; in directed graphs, they are arrows showing direction.
Use Cases
Undirected graphs are used when relationships are mutual, like in social networks where friendship is two-way. Directed graphs are used when relationships have direction, like web page links or task dependencies where one task must happen before another.
Choosing directed or undirected graphs depends on whether relationships are one-way or two-way.
Real World Analogy

Think of a neighborhood with streets. Some streets allow cars to go both ways, like undirected connections. Other streets are one-way, allowing cars to go only in one direction, like directed connections.

Undirected Graphs → Two-way streets where cars can travel back and forth freely
Directed Graphs → One-way streets where cars can only travel in the direction of the arrow
Edges and Arrows → Lines for two-way streets and arrows for one-way streets on a map
Use Cases → Choosing street types based on traffic rules and flow needs
Diagram
Diagram
  Undirected Graph       Directed Graph

  A─────B               A ──▶ B
   \   /                 
    C                   C     
                         
This diagram shows an undirected graph with lines connecting nodes both ways, and a directed graph with arrows showing one-way connections.
Key Facts
Undirected GraphA graph where edges have no direction and connections are mutual.
Directed GraphA graph where edges have a direction, showing one-way connections.
EdgeA connection between two nodes in an undirected graph.
ArcA directed edge with a specific direction from one node to another.
Use CaseThe practical situation where a directed or undirected graph is applied.
Code Example
Data Structures Theory
class Graph:
    def __init__(self, directed=False):
        self.directed = directed
        self.adj_list = {}

    def add_node(self, node):
        if node not in self.adj_list:
            self.adj_list[node] = []

    def add_edge(self, start, end):
        self.add_node(start)
        self.add_node(end)
        self.adj_list[start].append(end)
        if not self.directed:
            self.adj_list[end].append(start)

    def display(self):
        for node, neighbors in self.adj_list.items():
            print(f"{node} -> {neighbors}")

# Undirected graph example
g1 = Graph(directed=False)
g1.add_edge('A', 'B')
g1.add_edge('A', 'C')
g1.display()

print('---')

# Directed graph example
g2 = Graph(directed=True)
g2.add_edge('A', 'B')
g2.add_edge('A', 'C')
g2.display()
OutputSuccess
Common Confusions
Believing that undirected graphs can represent one-way relationships.
Believing that undirected graphs can represent one-way relationships. Undirected graphs always represent two-way connections; one-way relationships require directed graphs.
Thinking that edges and arcs are the same in all graphs.
Thinking that edges and arcs are the same in all graphs. Edges are undirected connections, while arcs are directed and show a clear direction.
Summary
Undirected graphs show two-way connections where movement is possible in both directions.
Directed graphs show one-way connections with arrows indicating direction from one node to another.
Choosing between directed and undirected graphs depends on whether relationships are mutual or one-sided.