Dsa - Graph
Dsa - Graph
A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes.
In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34, 04, 14, 13}.
Undirected Graph, Directed Graph,
Directed Graphs
A directed graph is a set of vertices (nodes) connected by edges, with each node having a direction associated with it.
Edges are usually represented by arrows pointing in the direction the graph can be traversed.
In the example on the right, the graph can be traversed from vertex A to B, but not from vertex B to A.
Undirected Graphs
In an undirected graph the edges are bidirectional, with no direction associated with them. Hence, the graph can be
traversed in either direction. The absence of an arrow tells us that the graph is undirected.
In the example on the left, the graph can be traversed from node A to B as well as from node B to A.
Graph terminology
In the above graph,
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
Vertex − Each node of the graph is represented as a vertex. In the following example, the labeled circle represents vertices.
Thus, A to G are vertices. We can represent them using an array as shown in the following image. Here A can be identified by
index 0. B can be identified using index 1 and so on.
Edge − Edge represents a path between two vertices or a line between two vertices. In the following example, the lines from
A to B, B to C, and so on represents edges. We can use a two-dimensional array to represent an array as shown in the
following image. Here AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and so on, keeping other
combinations as 0.
•Adjacency − Two node or vertices are adjacent if they are connected to each other through an edge. In the
following example, B is adjacent to A, C is adjacent to B, and so on.
•Path − Path represents a sequence of edges between the two vertices. In the following example, ABCD
represents a path from A to D.
Connectivity in undirected graph
Connected graph
Connected components
If graph is connected it has only one connected component which consist of the whole graph
Bridge
Articulation point
BiConnected Graph
BiConnected Components
Connectivity in directed graph
Strongly connected
Strongly connected components
weakly connected
Trees
Forest
Spanning Tree
Spanning Tree
If graph is connected then it will always have a Spanning Tree, If graph G is not connected then there will be
no Spanning Tree of G
Representation of graph: adjacency matrix, adjacency list,
Representation of graph:
The following two are the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of graph representation is situation-
specific. It totally depends on the type of operations to be performed and ease of use.
1 Adjacency Matrix
Adjacency Matrix for Graph a
2 Adjacency List
Undirected Graph (a)
Adjacency list for Graph(a)
Transitive closure of a Directed Graph and Path Matrix
Traversal in graph is different from traversal in tree or list bcz of the following reason
Traversal in Graph:
There are many methods of graph traversal but following two are popular:
In this technique ,first visit the starting vertex and then visit all the vertices adjacent to the Starting vertex.
After this we pick these adjacent vertices one by one and visit their adjacent vertices and this process goes on .
Here different traversal using different start vertex
Implementation of BFS using queue
During the algorithm, any vertex will be in one of the tree states-Initial, waiting, visited . At the start of the algorithm all
vertices will be in initial state, when a vertex will be inserted in the queue its state will change from initial to waiting.
When a vertex will be delected from queue and visited, its state will change from waiting to visited.
1. Insert the starting vertex into the queue, change its state to waiting.
2. Delete front element from the queue and visit it, change its state to visited.
3. Look for the adjacent vertices of the deleted element, and from these insert only those vertices into the queue which
are in the initial state. Change the state of all these inserted vertices from initial to waiting.
First the starting vertex is visited and then we have to pick up any path that starts from the starting vertex and visit all the
vertices in this path till we reach a dead end. This means visit the starting vertex say v1 and then any vertex adjacent to it
say v2. Now if v2 has any vertex adjacent to it which has not been visited then visit it. And so on till we come to dead end.
Dead end means that we reach a vertex which does not have any adjacent vertex or all of its adjacent vertices have been
visited.
After reaching the dead end we have backtrack along the path that we have visited till now.
0,1,2
Implementation of Depth First Search
Initially all vertices are at initial state, when vertex will be popped from stack its state will change to visited.
Procedure:
Initially stack is empty, and all vertices are in initial state:
1. Push starting vertex on the stack
2. Pop a vertex from stack
3. If popped vertex is in initial state, visit it and change its state to visited . Push all unvisited vertices adjacent to the
popped vertex.
4. Repeat steps 2 &3 until stack is empty.
There is no restriction on the order in which the successors of a vertex are visited and so we can push the successor of a
vertex in any order.
Here we are pushing the successors in descending order of their numbers. e.g. If the successors are 2,4,6 then we will
push 6 first and then 4 and then 2.
Depth First Search for given graph by using stack