Graph Representation
Graph Representation
Graph
Graph Representation
Representation
V={0,1,2,3,4}
E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, of ages 12, 15, 12,
15, 13, and 13, respectively.
E ={(x,y) | if x is younger than y}
Mary Helen
John Joe
Tom Paul
APPLICATIONS OF GRAPH
Graph Representation 6
CS 103
Adjacency matrix representation
Adjacency lists representation
Adjacency Matrix
Representation
In this representation, each graph of n nodes is represented by
an n x n matrix A, that is, a two-dimensional array A
The nodes are (re)-labeled 1,2,…,n
A[i][j] = 1 if (i,j) is an edge
A[i][j] = 0 if (i,j) is not an edge
Example of Adjacency Matrix
1
0 1 0 1 0 0
0 0 1 0 0
A= 2
0 0 1 0 0
1 0 0 0 0 4
0 0 0 4 0 3
Another Example of
Adj. Matrix
Re-label the nodes with numerical labels
Mary 0 Helen 1
0 0 0 0 0 0
0 0 0 0 0 0
A= Joe 3
1 1 0 0 1 1
John 2
1 1 0 0 1 1
1 1 0 0 0 0
1 1 0 0 0 0 Tom 4 Paul 5
Pros and Cons of
Adjacency Matrices
Pros:
Simple to implement
Easy and fast to tell if a pair (i,j) is an edge: simply check if A[i][j] is 1
or 0
Cons:
No matter how few edges the graph has, the matrix takes O(n 2) in
memory
Adjacency Lists
Representation
A graph of n nodes is represented by a one-dimensional array L
of linked lists, where
L[i] is the linked list containing all the nodes adjacent from node i.
The nodes in the list L[i] are in no particular order
Example of Linked Representation
L[0]: empty Mary 0 Helen 1
L[1]: empty
L[2]: 0, 1, 4, 5 Joe
John 2 3
L[3]: 0, 1, 4, 5
L[4]: 0, 1
Tom 4 Paul 5
L[5]: 0, 1
Pros and Cons of
Adjacency Lists
Pros:
Saves on space (memory): the representation takes as many
memory words as there are nodes and edge.
Cons:
It can take up to O(n) time to determine if a pair of nodes (i,j) is an
edge: one would have to search the linked list L[i], which takes time
proportional to the length of L[i].
The Graph Class
class Graph {
public:
typedef int datatype;
typedef datatype * datatypeptr;
Graph( int n=0); // creates a graph of n nodes and no edges
bool isEdge( int i, int j);
void setEdge( int i, int j, datatype x);
int getNumberOfNodes(){return numberOfNodes;};
private:
datatypeptr *p; //a 2-D array, i.e., an adjacency matrix
int numberOfNodes;
};
Graph Class Implementation
V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, where the first 4 are
siblings, and the last two are siblings
E ={(x,y) | x and y are siblings}
if (x,y) is an edge:
Mary Helen we say that x is
adjacent to y, &
Joe y adjacent to x.
John
We also say that
Tom Paul x and y are
neighbors
Representations of
Undirected Graphs
The same two representations for directed graphs can be used
for undirected graphs
Adjacency matrix A:
A[i][j]=1 if (i,j) is an edge; 0 otherwise
Adjacency Lists:
L[i] is the linked list containing all the neighbors of i
Example of Representations
Connected
Disconnected
Graph Traversal Techniques
In both DFS and BFS, the nodes of the undirected graph are
visited in a systematic manner so that every node is visited
exactly one.
Both BFS and DFS give rise to a tree:
When a node x is visited, it is labeled as visited, and it is added to
the tree
If the traversal got to node x from node y, y is viewed as the parent
of x, and x a child of y
Depth-First Search
DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the
new current node;
4. Repeat steps 3 and 4 until no more nodes can be visited.
5. If there are still unvisited nodes, repeat from step 1.
Illustration of DFS
0 1
0
2
1 4
4 9
10 5 7
2
11 8
5 9
6
7 11
6 Graph G
8 10
DFS Tree
Implementation of DFS
Observations:
the last node visited is the first node from which to proceed.
Also, the backtracking proceeds on the basis of "last visited, first to backtrack
too".
This suggests that a stack is the proper data structure to remember the current
node and how to backtrack.
Illustrate DFS with a Stack
We will redo the DFS on the previous graph, but this time with
stacks
In Class
DFS (Pseudo Code)
DFS(input: Graph G) {
Stack S; Integer x, t;
while (G has an unvisited node x){
visit(x); push(x,S);
while (S is not empty){
t := peek(S);
if (t has an unvisited neighbor y){ visit(y);
push(y,S); }
else
pop(S);
}
}
}
Breadth-First Search
BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root
in a BFS tree being formed. Its level is called the
current level.
2. From each node z in the current level, in the order in
which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes from
this level form a new level that becomes the next
current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
Illustration of BFS
0 1
0
2 4 2
1 4
9
5 9 10
10 5 7
11 8
6 7 8 11
6
BFS Tree Graph G
Implementation of DFS
Observations:
the first node visited in each level is the first node from which to proceed to visit
new nodes.
This suggests that a queue is the proper data structure to remember the
order of the steps.
Illustrate BFS with a Queue
We will redo the BFS on the previous graph, but this time with
queues.
BFS (Pseudo Code)
BFS(input: graph G) {
Queue Q; Integer x, z, y;
while (G has an unvisited node x) {
visit(x); Enqueue(x,Q);
while (Q is not empty){
z := Dequeue(Q);
for all (unvisited neighbor y of z){
visit(y); Enqueue(y,Q);
}
}
}
}
Complexity Analysis of BFS &
DFS
Time Complexity:
•DFS explores as far along a branch as possible before backtracking,
visiting each vertex and edge once.
•Like BFS, if the graph is represented as an adjacency list:
• For each vertex, DFS will traverse its neighboring vertices
(adjacent edges).
•Overall Time Complexity:
•O(V+E)O(V + E) where:
• V is the number of vertices.
• E is the number of edges.
Space Complexity:
• DFS typically uses a stack (either explicitly, or through recursion)
to keep track of vertices.
• In the case of a recursive DFS, the maximum depth of recursion
corresponds to the maximum depth of the graph.
• The depth could go up to V in the worst case.
• Overall Space Complexity:
• O(V) as it requires space for the stack (or recursion) and a visited
list to track the visited vertices.