Graph (Graph DS, BFS, DFS, Prim's, Krushkal's) PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

Graph Data Structure

Graph
A graph is a group of vertices and edges connecting those vertices. A graph can be seen as a cyclic tree,
where the vertices (nodes) have complex relationship among them instead of parent child relationship.

Definition
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of vertices and E(G)
represents the set of edges which are used to connect these vertices.

Example
A Graph G(V, E) with 5 vertices (A, B, C, D, E)
and six edges ((A,B), (B,C), (C,E), (E,D), (D,B),
(D,A)) is shown in the following figure.
Directed and Undirected Graph
A graph can be directed or undirected. In an undirected graph, edges are not associated with the directions with
them. Therefore, if an edge exists between vertex A and B then the vertices can be traversed from B to A as well
as from A to B.

In a directed graph, edges form an ordered pair. Edges represent a specific path from some vertex A to another
vertex B. Node A is called initial node while node B is called terminal node.
In directed graph, Edge(A, B) ≠ Edge (B, A).

Directed Graph
Undirected Graph
Graph Terminology

Path
A path can be defined as the sequence of nodes that are followed in order to reach some terminal node V from the
initial node U.

In the given graph, the path from A to E is Path(A,E) = A--->B--->C--->E.

Closed Path
A path will be called as closed path if the initial node is same as terminal node.
In the given graph, the path A--->B--->D--->A is a closed path.

Cycle
A cycle can be defined as the path which has no repeated edges or vertices except the first and last
vertices.
Connected Graph
A connected graph is the one in which some path exists between every two vertices (u, v) in V. There are no
isolated nodes in connected graph.

Connected Graph
Disconnected Graph
Complete Graph

A complete graph is the one in which every node is connected with all other nodes. A complete graph contain n(n-
1)/2 edges where n is the number of nodes in the graph.

No of edges = n(n-1)/2
Where n is the number
of nodes in the graph
Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or weight. The weight of an edge e can
be given as w(e) which must be a positive (+) value indicating the cost of traversing the edge.
Digraph
A digraph is a directed graph in which each edge of the graph is associated with some direction and the
traversing can be done only in the specified direction.
Degree of the vertex
A degree of a vertex/node is the number of edges that are connected with that node. A node with degree 0 is
called as isolated node

Node A has degree 5.


G has degree 2.
C has degree 3 and so on.

In an undirected graph, each node


has both indegree and outdegree.
Indegree of a vertex
Indegree of a vertex is the number of edges incoming to the vertex.

Indegree (0) = 1.
Indegree (1) = 2
Indegree (2) = 1
Indegree (3) = 2
Indegree (4) = 1

Outdegree of a vertex
Outdegree of a vertex is the number of edges outgoing from the vertex.

Outdegree (0) = 1.
Outdegree (1) = 2
Outdegree (2) = 1
Outdegree (3) = 2
Outdegree (4) = 1
Parallel Edges
In graph theory, multiple edges (also called parallel edges or a multi-edge), are, in an undirected graph, two
or more edges that are incident to the same two vertices, or in a directed graph, two or more edges with both
the same tail vertex and the same head vertex.
Loop (Self-loop)
In graph theory, a loop (also called a self-loop or a buckle) is an edge that connects a vertex to itself. A simple
graph contains no loops.
Simple Graphs and Multigraphs
• A simple graph, also called a strict graph is an unweighted, undirected graph containing no graph loops or
multiple edges.
• A multi-graph contains parallel edges and/or self loops.

Simple Graph Multi-Graph


Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called as neighbors or adjacent nodes.

In the given graph, adjacent vertices are:

(0, 1)
(1, 4)
(0, 3)
(3, 4)
(1, 2)
(2, 3)
(1, 3)
Graph Representation

• Graph representation means the technique which is to be used to store graph into the computer's memory.

• There are two following ways to store Graph into the computer's memory:

➢ Sequential Representation (Adjacency Matrix Representation)


➢ Linked Representation
Sequential Representation
In sequential representation, we use adjacency matrix to store the mapping represented by vertices and edges.
In adjacency matrix, the rows and columns are represented by the graph vertices. A graph having n vertices,
will have a dimension n x n.

An entry Aij in the adjacency matrix representation of an undirected graph G will be 1 if there exists an edge
between Vi and Vj.
There exists different adjacency matrices for the directed and undirected graph. In directed graph, an entry Aij
will be 1 only when there is an edge directed from Vi to Vj.

A directed graph and its adjacency matrix representation is shown in the following figure.
Representation of weighted directed graph is different. Instead of filling the entry by 1, the Non- zero entries of
the adjacency matrix are represented by the weight of respective edges.

The weighted directed graph along with the adjacency matrix representation is shown in the following figure.
Linked Representation
In the linked representation, an adjacency list is used to store the Graph into the computer's memory.

Consider the undirected graph shown in the following figure and check the adjacency list representation.
An adjacency list is maintained for each node present in the graph which stores the node value and a pointer to
the next adjacent node to the respective node. If all the adjacent nodes are traversed then store the NULL in the
pointer field of last node of the list. The sum of the lengths of adjacency lists is equal to the twice of the number
of edges present in an undirected graph.
Consider the directed graph shown in the following figure and check the adjacency list representation of the graph.

In a directed graph, the sum of lengths of all the adjacency lists is equal to the number of edges.
In the case of weighted directed graph, each node contains an extra field that is called the weight of the node.
The adjacency list representation of a directed graph is shown in the following figure.
Graph Traversal

Traversing the graph means examining all the nodes and vertices of the graph. There are two standard methods
by using which, we can traverse the graphs. Lets discuss each one of them in detail.

• Breadth First Search


• Depth First Search
Breadth First Search (BFS) Algorithm

• Breadth first search is a graph traversal algorithm that starts traversing the graph from root node and
explores all the neighboring nodes. Then, it selects the nearest node and explore all the unexplored
nodes. The algorithm follows the same process for each of the nearest node until it finds the goal.

• The algorithm of breadth first search is given below. The algorithm starts with examining the node A
and all of its neighbors. In the next step, the neighbors of the nearest node of A are explored and
process continues in the further steps. The algorithm explores all neighbors of all the nodes and
ensures that each node is visited exactly once and no node is visited twice.
Example
Consider the graph G shown in the following image, calculate the minimum path p from node A to node E. Given
that each edge has a length of 1.

Solution
Minimum Path P can be found by applying breadth first search algorithm that will begin at node A and will end
at E. the algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes that are to
be processed while QUEUE2 holds all the nodes that are processed and deleted from QUEUE1.
Lets start examining the graph from Node A.

1. Add A to QUEUE1 and NULL to QUEUE2.

QUEUE1 = {A}
QUEUE2 = {NULL}

2. Delete the Node A from QUEUE1 and insert all its neighbors. Insert Node A into QUEUE2

QUEUE1 = {B, D}
QUEUE2 = {A}

3. Delete the node B from QUEUE1 and insert all its neighbors. Insert node B into QUEUE2.

QUEUE1 = {D, C, F}
QUEUE2 = {A, B}
4. Delete the node D from QUEUE1 and insert all its neighbors. Since F is the only neighbor of it which
has been inserted, we will not insert it again. Insert node D into QUEUE2.

QUEUE1 = {C, F}
QUEUE2 = { A, B, D}

5. Delete the node C from QUEUE1 and insert all its neighbors. Add node C to QUEUE2.

QUEUE1 = {F, E, G}
QUEUE2 = {A, B, D, C}

6. Remove F from QUEUE1 and add all its neighbors. Since all of its neighbors has already been added,
we will not add them again. Add node F to QUEUE2.

QUEUE1 = {E, G}
QUEUE2 = {A, B, D, C, F}
7. Remove E from QUEUE1, all of E's neighbors has already been added to QUEUE1 therefore we will not
add them again. All the nodes are visited and the target node i.e. E is encountered into QUEUE2.

QUEUE1 = {}
QUEUE2 = {A, B, D, C, F, E, G}

Now, backtrack from E to A, using the nodes available in QUEUE2.

BFS sequence: A, B, D, C, F, E, G
Q. Find the BFS traversal of the given graph.

Solution:
Lets start examining the graph from Node H.

1. Add H to QUEUE1 and NULL to QUEUE2.

QUEUE1 = {H}
QUEUE2 = {NULL}

2. Delete the Node H from QUEUE1 and insert all its neighbors. Insert Node H into QUEUE2

QUEUE1 = {A}
QUEUE2 = {H}

3. Delete the node A from QUEUE1 and insert all its neighbors. Insert node A into QUEUE2.

QUEUE1 = {B, D}
QUEUE2 = {H, A}
4. Delete the node B from QUEUE1 and insert all its neighbors.
Insert node B into QUEUE2.

QUEUE1 = {D, C, F}
QUEUE2 = {H, A, B}

5. Delete the Node D from QUEUE1 and insert all its neighbors.
Insert Node D into QUEUE2

QUEUE1 = {C, F}
QUEUE2 = {H, A, B, D}

6. Delete the node C from QUEUE1 and insert all its neighbors.
Insert node C into QUEUE2.

QUEUE1 = {F, G, E}
QUEUE2 = {H, A, B, D, C}
7. Delete the node F from QUEUE1 and insert all its neighbors.
Insert node F into QUEUE2.

QUEUE1 = {G, E}
QUEUE2 = {H, A, B, D, C, F}

8. Delete the Node G from QUEUE1 and insert all its neighbors.
Insert Node G into QUEUE2

QUEUE1 = {E}
QUEUE2 = {H, A, B, D, C, F, G}

9. Delete the node E from QUEUE1 and insert all its neighbors.
Insert node E into QUEUE2.

QUEUE1 = {NULL}
QUEUE2 = {H, A, B, D, C, F, G, E}

BFS sequence: H, A, B, D, C, F, G, E
Depth First Search (DFS) Algorithm

• Depth first search (DFS) algorithm starts with the initial node of the graph G, and then goes to deeper and
deeper until we find the goal node or the node which has no children. The algorithm, then backtracks from
the dead end towards the most recent node that is yet to be completely unexplored.

• The data structure which is being used in DFS is stack. The process is similar to BFS algorithm. In DFS, the
edges that leads to an unvisited node are called discovery edges while the edges that leads to an already
visited node are called block edges.
Example
Consider the graph G along with its adjacency list, given in the figure below. Calculate the order to print all
the nodes of the graph starting from node H, by using depth first search (DFS) algorithm.
Solution

Push H onto the stack

STACK : H
POP the top element of the stack i.e. H, print it and push all the neighbors of H onto the stack that are in
ready state.

Print H
STACK : A
Pop the top element of the stack i.e. A, print it and push all the neighbors of A onto the stack that are in ready
state.

Print A
Stack : B, D
Pop the top element of the stack i.e. D, print it and push all the neighbors of D onto the stack that are in ready
state.

Print D
Stack : B, F
Pop the top element of the stack i.e. F, print it and push all the neighbors of F onto the stack that are in ready
state.

Print F
Stack : B
Pop the top of the stack i.e. B and push all the neighbors

Print B
Stack : C
Pop the top of the stack i.e. C and push all the neighbors.

Print C
Stack : E, G
Pop the top of the stack i.e. G and push all its neighbors.

Print G
Stack : E
Pop the top of the stack i.e. E and push all its neighbors.
Print E
Stack :
Hence, the stack now becomes empty and all the nodes of the graph have been traversed.

The printing sequence of the graph will be :

H→A→D→F→B→C→G→E

DFS sequence: H, A, D, F, B, C, G, E
Q. Find the DFS traversal of the given graph starting with A.

Solution
Push A onto the stack

STACK : A
POP the top element of the stack i.e. A, print it and push all the
neighbors of A onto the stack that are in ready state.

Print A {A}
STACK : B, C, D
Pop the top element of the stack i.e. D, print it and push all the
neighbors of D onto the stack that are in ready state.

Print D {A, D}
Stack : B, C
Pop the top element of the stack i.e. C, print it and push all the
neighbors of C onto the stack that are in ready state.
Print C {A, D, C}
Stack : B, F
Pop the top element of the stack i.e. F, print it and push all the
neighbors of F onto the stack that are in ready state.

Print F {A, D, C, F}
Stack : B
Pop the top element of the stack i.e. B, print it and push all the
neighbors of B onto the stack that are in ready state.

Print B {A, D, C, F, B}
Stack : E
Pop the top element of the stack i.e. E, print it and push all the
neighbors of E onto the stack that are in ready state.

Print E {A, D, C, F, B, E}
Stack : Empty

BFS sequence: A, D, C, F, B, E
Topological Sorting (Ordering)

• Topological sorting is done on Directed Acyclic graph (DAG). The graph should be directed and shouldn’t
contain any cycle.
• Topological sorting is the linear ordering of the vertices of DAG such that for every directed edge(u, v)
from u---->v, u comes before vertex v in the ordering.
• Every DAG will have atleast one topological ordering.

Procedure
1. Calculate the indegree of all vertex in the graph.
2. Choose the vertex V0 having indegree zero.
3. Now delete all the outgoing vertex from vertex V0.
4. Now delete the vertex V0 and recalculate the indegrees of remaining vertices.
5. Repeat the same procedure until we have the null graph.
Consider the given graph to find topological ordering. 2
1
2 3
1. First compute the indegrees of all vertices.
0
2. Select the vertex having indegree 0, i.e. vertex 1. 1
1
3. Delete all the outgoing edges from vertex 1. 4 5
2
4. Now delete the vertex 1 and recalculate the indegrees of
remaining vertices.
0 1
2 2
Topological Ordering 2 3 2 3
0
{1}
1

4 5 1 4 5 1
1 2
Again Choose the vertex having indegree zero, i.e. 2. 0
2
2 3
Delete all the outgoing edges from vertex 2.

0
2
2 3 4 5 1
1

4 5 1 1
3
1

Now delete the vertex 2 and recalculate the indegrees of remaining


vertices.
4 5 1

Topological Ordering {1, 2} 0


Again Choose the vertex having indegree zero, i.e. 4. 1
3
Delete all the outgoing edges from vertex 4.

1
3 1
4 5
0

4 5 1
0
3
0

Now delete the vertex 4 and recalculate the indegrees of remaining vertices.
5 0

Topological Ordering {1, 2, 4}


Again Choose the vertex having indegree zero. But now both the remaining vertices have indegree zero. In such
case we may consider either of these two in the ordering.

In such case, two ordering may be found as follows:

{1, 2, 4, 3, 5} and {1, 2, 4, 5, 3}


Example2. Lets take a Directed Cyclic graph and try to find the topological ordering.
2 0
Here vertices 3 and 5 both have indegree 0. We can select any
2 3
vertex. Lets begin with 5 and delete all outgoing edge of 5.
1
Now delete 5 and recalculate the indegrees of all vertices.
1
0
Topological ordering = {5}
4 5
Now take vertex 3 and delete all outgoing edge of 3. Now 2
delete 3 and recalculate the indegrees of all vertices.
Topological ordering = {5, 3} 2 0
1
2 3
At this point, we don’t have 2
any vertex having indegree 0. 1
1
So we cannot proceed further. 1
1
It means if graph contains 4
4
cycle, we cannot find the
topological ordering. 1
1
Find the topological ordering for the given graph.

{a, b, c, d, e, g, f, h}
Spanning Tree

A spanning tree is a sub-graph of an undirected connected graph, which includes all the vertices of the graph with
a minimum possible number of edges. If a vertex is missed, then it is not a spanning tree.
The edges may or may not have weights assigned to them.

If a graph G(V, E) is given, its spanning tree will contain V vertices and edges equal to |V|-1.

The total number of spanning trees with n vertices that can be created from a complete graph is equal to n(n-2).

If we have n = 4, the maximum number of possible spanning trees is equal to 44-2 = 16. Thus, 16 spanning trees
can be formed from a complete graph with 4 vertices.
Example of a Spanning Tree
Minimum (Cost) Spanning Tree

A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, undirected graph
is a spanning tree with a weight less than or equal to the weight of every other spanning tree. The weight of a
spanning tree is the sum of weights given to each edge of the spanning tree.
Properties of Spanning Tree:
• There may be several minimum spanning trees of the graph edges of same weight.

• If all the edge weights of a given graph are the same, then every spanning tree of that graph is minimum.

• If each edge has a distinct weight, then there will be only one, unique minimum spanning tree.

• A connected graph G can have more than one spanning trees.

• A disconnected graph can't have to span the tree, or it can't span all the vertices.

• Spanning Tree doesn't contain cycles.

• Spanning Tree has (n-1) edges where n is the number of vertices.

• Addition of even one single edge results in the spanning tree losing its property of Acyclicity and

elimination of one single edge results in its losing the property of connectivity.
Example of a Spanning Tree for weighted graph

Given graph
Algorithms to find Minimum (Cost) Spanning Tree

The minimum spanning tree from a graph is found using the following algorithms:

• Prim's Algorithm
• Kruskal's Algorithm
Prim’s Algorithm

Prim's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the subset of the
edges of that graph which
➢ form a tree that includes every vertex
➢ has the minimum sum of weights among all the trees that can be formed from the graph

How Prim's algorithm works

• We start from one vertex and keep adding edges with the lowest weight until we reach our goal.
• The steps for implementing Prim's algorithm are as follows:
➢ Initialize the minimum spanning tree with a vertex chosen at random.
➢ Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree
➢ Keep repeating until we get a minimum spanning tree
Example of Prim's algorithm

Start with the given weighted graph


Choose the shortest edge from this vertex and add it
Minimum cost spanning Tree
Find out minimum spanning tree for the given graph using Prim’s Algorithm.
Krushkal’s Algorithm

• We start from the edges with the lowest weight and keep adding edges until we reach our goal.
• The steps for implementing Kruskal's algorithm are as follows:
➢ Sort all the edges from low weight to high
➢ Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle,
then reject this edge.
➢ Keep adding edges until we reach all vertices.
Example of Krushkal’s algorithm

Start with the given weighted graph

A D
4 3 3
4
4 C F

2 2
B 3
E
C
C
2
B 2
2 E
B

Step 1 Step 2

D
3

F
C

2
B 2
E

Step 3
D
3
3
F
C

2
B 2
E

Step 4

D
A
3
4 3
F
C

2
B 2 Cost = 2+2+3+3+4
E
= 14
Step 5
Find out minimum spanning tree for the given graph using Krushkal’s Algorithm.

You might also like