8-Graph ADA
8-Graph ADA
Graphs
What is Graph?
Graph Terminologies
Properties of Graph
Representation of Graph
Matrix representation of Graph
Linked List representation of Graph
Elementary Graph Operations
Breadth First Search (BFS)
Depth First Search (DFS)
Topological Sort
Source Removal Algorithm
Bi-Connected Components
Non Linear Data Structures (Graph) 2
What is Graph ?
• A Graph is a non-linear data structure
consisting of vertices and edges. The
vertices are sometimes also referred to as
nodes and the edges are lines or arcs that
connect any two nodes in the graph. More
formally a Graph is composed of a set of
vertices( V ) and a set of edges( E ). The
graph is denoted by G(E, V).
1 𝑖𝑓 𝑉𝑖 , 𝑉𝑗 ∈ 𝐸
aij = ቊ
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
V1 V4 V1 V2 V3 V4
V1 0 1 0 1
A = V2 1 0 0 0
V3 1 1 0 1
V4 0 1 0 0
V2 V3
The number of elements in the ith row whose value is 1 is equal to the out-degree of node Vi
The number of elements in the jth column whose value is 1 is equal to the in-degree of node Vj
For a NULL graph which consist of only n nodes but no edges, the adjacency matrix has all its
elements 0. i.e. the adjacency matrix is the NULL matrix
Entry of 1 in ith row and jth column of A shows existence of an edge (Vi, Vj), that is a path of length 1
Entry in A2 shows no of different paths of exactly length 2 from node Vi to Vj
Entry in A3 shows no of different paths of exactly length 3 from node Vi to Vj
4
1
2
3
0 1 2 3 4
1 0 3
2 0 3 4
3 0 1 2 4
4 0 2 3
8 1 2 6 3 8 7 4 5
Non Linear Data Structures (Graph) 16
Depth First Search (DFS)
✓ A
✓ B C ✓
M N O
✓ D ✓ E F ✓ G ✓
R
Q P
H
✓
ABDHECFG
✓ A
✓
✓ B ✓ C E A B D C F E
✓ D F✓
Non Linear Data Structures (Graph) 17
Breadth First Search (BFS)
This methods starts from vertex V0
V0 is marked as visited. All vertices adjacent to V0 are visited next
Let vertices adjacent to V0 are V1, V2, V2, V4
V1, V2, V3 and V4 are marked visited
All unvisited vertices adjacent to V1, V2, V3, V4 are visited next
The method continuous until all vertices are visited
The algorithm for BFS has to maintain a list of vertices which have been visited but not
explored for adjacent vertices
The vertices which have been visited but not explored for adjacent vertices can be stored in
queue
6 ✓ 7 ✓
H✓
✓ 8 1 |2 3 4 5| 6 7| 8 A|BC |DEFG |H
V4
V1
V0| V1 V2 | V4 V6 V3 | V5
V2
V0 V6
V3
V5
A
B C
M N O
D E F G
R
Q P
H
A A
0 1
B C E B E
5 2
D F
4 3
C D
Non Linear Data Structures (Graph) 20
Procedure : DFS (vertex V)
This procedure traverse the graph G in DFS manner.
V is a starting vertex to be explored.
Visited[] is an array which tells you whether particular vertex is visited or not.
W is a adjacent node of vertex V.
S is a Stack, PUSH and POP are functions to insert and remove from stack respectively.
In topological sorting,
We use a temporary stack.
we first recursively call topological sorting for all its adjacent vertices, then push it to a stack.
Note: A vertex is pushed to stack only when all of its adjacent vertices (and their adjacent vertices and so on) are
already in the stack
A simple approach is to one by one remove all vertices and see if removal of a vertex causes
disconnected graph.
This method is the easiest approach. The basic idea is to iteratively remove a node at a time from the graph
and see which nodes cause the graph to be compartmentalized.
Even though this method is effective at finding articulation point, as it should be, it has the high time
complexity as the tree has to be traversed each time per node.
The time complexity = O(v*(v+e))
The idea is to use DFS (Depth First Search). In DFS, follow vertices in a tree form called the DFS
tree. In the DFS tree, a vertex u is the parent of another vertex v, if v is discovered by u.
In DFS tree, a vertex u is an articulation point if one of the following two conditions is true.
u is the root of the DFS tree and it has at least two children.
u is not the root of the DFS tree and it has a child v such that no vertex in the subtree rooted with v has a
back edge to one of the ancestors in DFS tree of u.
i. If `v` is not visited, set `v` as the parent of `u` and perform a DFS from `v`.
ii. After returning from the DFS call for `v`, update the `low[u]` value as the minimum of `low[u]` and `low[v]`.
iii. If `u` is not the root node and `low[v] >= discovery_time[u]`, then `u` is an articulation point.
iv. If `u` is the root node and has at least two children in the DFS tree, then `u` is an articulation point.