0% found this document useful (0 votes)
6 views

Graph

The document discusses graph representations, specifically adjacency matrices and adjacency lists, detailing their advantages and disadvantages. It also covers traversal algorithms like Breadth First Search (BFS) and Depth First Search (DFS), as well as concepts related to spanning trees and minimum spanning trees (MST) using Kruskal's and Prim's algorithms. Additionally, it introduces Dijkstra's algorithm for finding the shortest path in weighted directed graphs.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Graph

The document discusses graph representations, specifically adjacency matrices and adjacency lists, detailing their advantages and disadvantages. It also covers traversal algorithms like Breadth First Search (BFS) and Depth First Search (DFS), as well as concepts related to spanning trees and minimum spanning trees (MST) using Kruskal's and Prim's algorithms. Additionally, it introduces Dijkstra's algorithm for finding the shortest path in weighted directed graphs.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

GRAPH

Graph Representation

The following two are the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List

Adjacency matrix Representation


Adjacency matrix, also called bit matrix or Boolean matrix. If an edge exists between
two nodes it stores true (represented by 1) otherwise false (represented by 0).
True or 1 if edge exist between I to j
v[i][j]
False or 0 otherwise
Example (Directed Graph)

Graph Adjacency matrix Representation


1

Graph Adjacency matrix Representation


In above diagram A to B edge exist so in the matrix A to B is 1. A to C edge does not exist so in
the matrix A to C is 0.

Points to Remember-
In case of directed Graph, the sum of matrix is always equal to number of edges|E| or sum of in-degree of each vertex.
In above diagram
Number of edges= 7
Sum of matrix = 7
Number of edges = Sum of Matrix
Example (Undirected Graph)
Graph Adjacency matrix Representation

Points to Remember-
In case of undirected Graph, the sum of matrix is always equal to twice the number of edges i.e. 2|E| or sum of degree of
each vertex.

In above diagram , Number of edges= 9,


Sum of matrix= 18
Sum of matrix=2|E|
Example (Weighted Directed
Graph)

Graph Weight matrix Representation


In above diagram A to B edge exist so in the matrix A to B is 2. A to C edge does not exist so in the matrix A to C is 0.
Example (Weighted Undirected
Graph)
Example (Weighted Undirected
Graph)
Disadvantage of Adjacency matrix
No. of entries for directed graph= e
No. of entries for undirected graph= 2e
where e= edges of graph
total entries in matrix= n X n
where n=no. of vertex
Memory wasted for directed graph=n2 -e
Memory wasted for undirected graph= n2 -2e
Advantage of adjacency matrix
• Used efficiently for more entries
• Traversal or accessing of any node is easy
• Adjacency List:
An array of lists is used. The size of the array is equal to the number of
vertices. Let the array be an array[]. An entry array[i] represents the
list of vertices adjacent to the ith vertex.
• This representation can also be used to represent a weighted graph.
The weights of edges can be represented as lists of pairs.
Disadvantage of adjacency list
• Checking the existence of an edge between two vertices i.e. i and e ,
is time consuming
• Calculations of the degree of the vertex is also time consuming
traversal algorithms
The graph has two types of traversal algorithms. These are called
• Breadth First Search (BFS)
• Depth First Search (DFS)
Traversal algorithms: Breadth First
Search (BFS)
During the execution of our algorithms, each node N of G will be one of
three states, called the status of N, as follows:

STATUS=1: (Ready state) The initial state of the node N


STATUS =2: (Waiting state) The node N is on the Queue ,waiting to be processed
STATUS=3: (Processed state) The Node n has been processed
This algorithm executes a BFS on a Graph G beginning at a starting node A
Step1: Initialize all nodes to the ready state (STATUS=1).
Step 2 : Put the starting node A in QUEUE and change its status to the
waiting state (STATUS=2).
Step3: Repeat steps 4 & 5 until QUEUE is empty:
Step4: Remove the front node N of QUEUE . Process N and change
the status of N to the processed state (STATUS=3).
Step5: Add to the rear of QUEUE all the neighbors of N
that are in the steady state (STATUS=1) and change their
status to the waiting state (STATUS=2).
[End of Step3 loop]
Step6: Exit
Traversal algorithms: Depth First
Search (DFS)
During the execution of our algorithms, each node N of G will be one of
three states, called the status of N, as follows:

STATUS=1: (Ready state) The initial state of the node N


STATUS =2: (Waiting state) The node N is on the Stack ,waiting to be processed
STATUS=3: (Processed state) The Node n has been processed
This algorithm executes a BFS on a Graph G beginning at a starting node A
Step1: Initialize all nodes to the ready state (STATUS=1).
Step 2 : Put the starting node A in STACK and change its status to the
waiting state (STATUS=2).
Step3: Repeat steps 4 & 5 until STACK is empty:
Step4: Pop the top node N of STACK . Process N and change the
status of N to the processed state (STATUS=3).
Step5: Push on to STACK all the neighbors of N that are in the
steady state (STATUS=1) and change their status to the
waiting state (STATUS=2).
[End of Step3 loop]
Step6: Exit
Spanning Tree
A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges. Hence, a spanning
tree does not have cycles and it cannot be disconnected.
We found three spanning trees off one complete graph. A complete
undirected graph can have maximum n(n-2) number of spanning trees,
where n is the number of nodes. In the above addressed example, n is
3, hence 3(3−2) = 3 spanning trees are possible.
General Properties of Spanning Tree
We now understand that one graph can have more than one spanning tree. Following
are a few properties of the spanning tree connected to graph G −

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

• All possible spanning trees of graph G, have the same number of edges and vertices.

• The spanning tree does not have any cycle (loops).

• Removing one edge from the spanning tree will make the graph disconnected, i.e. the
spanning tree is minimally connected.
Minimum Spanning Tree (MST)
In a weighted graph, a minimum spanning tree is a spanning tree that has minimum
weight than all other spanning trees of the same graph. In real-world situations,
this weight can be measured as distance, congestion, traffic load or any arbitrary
value denoted to the edges.
We shall learn about two most important spanning tree algorithms here −

Kruskal's Algorithm

Prim's Algorithm

Both are greedy algorithms.


Prim’s Minimum Spanning Tree and
Kruskal’s algorithm fails for directed graphs
• Prim’s algorithm assumes that all vertices are connected. But in a
directed graph, every node is not reachable from every other node.
So, Prim’s algorithm fails due to this reason.

As it is visible in the graph, no node is reachable from node 4. Directed


graphs fail the requirement that all vertices are connected.
Why Kruskal’s Algorithm fails for directed graph ?
• In Kruskal’s algorithm, In each step, it is checked that if the edges form a cycle
with the spanning-tree formed so far. But Kruskal’s algorithm fails to detect the
cycles in a directed graph as there are cases when there is no cycle between
the vertices but Kruskal’s Algorithm assumes it to cycle and don’t take consider
some edges due to which Kruskal’s Algorithm fails for directed graph.
For example:
Finding MST?

Kruskal's Algorithm Arrange all the edges in ascending


order to their weight.
Finding MST?

Kruskal's Algorithm Arrange all the edges in ascending


order to their weight.
edge weight
(7,6) 1
(5,6) 2
Finding MST?

Arrange all the edges in ascending


order to their weight.
edge weight
(6,7) 1
(6,5) (2,8) 2
(2,5)(0,1) 4
(8,6) 6
(2,3)(7,8) 7
(1,2) (0,7) 8
(3,4) 9
(5,4) 10
(1,7) 11
(3,5) 14

Total cost= 37
0
5
2

2
1 2

6 7
3

25 10

14

12 5
4

11 15

6
0
0
5 2
2
2
2 1 2
1 2
6
6 7
3
3

10
25 10

14

12 5
12 4
4 5

11
11 15 Total cost:43
6
6
Kruskal’s algorithm
Step1: set minimum spanning tree (MST) initially empty.
A=Ø
Step2: For each vertex V Ɛ V(G)
create (V) no. of tree where tree contain all vertex no edge.
Step3: Set the edges into ascending order according to their weights
and select the edge which has minimum weight.
Step4: if that forms cycles rejected it otherwise add it.
Step5: repeat until the single tree is formed.
Step 6: return MST.
Prims Algorithm
For a Graph G={V,E}
Assume S= Set of vertices in MST
A= Set of edges in MST
Step1: select any vertex ‘r’ and set S={r} and also set A= {Ø}
Step2: Find the smallest weight edge such that one end point in S and other in
{ V-S}.
Step3: Now add this edge into the set A and set another point into set S
Step 4:IF {V-S} =Ø then
Stop
and output is return i.e. MST
otherwise goto Step2.
0 Set of Vertices Edges Selected edges 0
5 in MST (less weight)
2 2
2 { 0} (0,1)(0,2)
1 2 2

6 7
3

25 10

14

12 5
4

11 15

6
0 Set of Vertices Edges Selected 0
5 in MST edges
2 (less 2
weight)
2 2
1 2 1 2
{ 0} (0,1)(0,2) (0,2)
6 7
3 {0,2} (0,1)(2,1)(2,3)(2,5) (2,1)

{0,2,1}
25 10

14

12 5
4

11 15

6
0 Set of Vertices Edges Selected 0
5 in MST edges
2 (less 2
weight)
2 2
1 2 1 2
{ 0} (0,1)(0,2) (0,2)
6 7 6
3 {0,2} (0,1)(2,1)(2,3)(2,5) (2,1)
3

{0,2,1} (2,3)(2,5)(1,3)(1,4) (1,3)


25 10

14

12 5
4

11 15

6
0 Set of Vertices Edges Selected 0
5 in MST edges
2 (less 2
weight)
2 2
1 2 1 2
{ 0} (0,1)(0,2) (0,2)
6 7 6
3 {0,2} (0,1)(2,1)(2,3)(2,5) (2,1) 3

{0,2,1} (2,3)(2,5)(1,3)(1,4) (1,3)


25 10 10
{0,2,1,3} (2,5)(1,4)(3,6) (2,5)
14
{0,2,1,3,5} (1,4)(3,6)(5,4)(5,6) (5,4)
12 5 12
4 {0,1,2,3,4,5} (3,6)(5,6)(4,6) (4,6) 4 5

11 15 {0,1,2,3,4,5,6} - - 11

6 6
Total cost=2+2+6=10+12+11= 43
Single Source Shortest path
For a given Graph G= (V, E), we want to find a shortest path from a give
source vetex S Ɛ V o every vertex v Ɛ V . A single source shortest path
may be computed with the following algorithms.

1. Dijkstra's Algorithm
Dijkstra's Algorithm
Dijakstra’s algo solves the single source shortest path problem on a
weighted , directed graph G=(V,E) for the case in which all edge
weights are non negative.
The Dijaksta’s algorithm uses a greedy strategies i.e. it always select
the lightest or closest vertex.
Find the shortest path from source
vertex to all other Source
vertexvertex S=0
S=Finally contains vertex of final shortest path
Q= Priority queue used to store vertices based on
1 v0 their distance from source vertex
u0
10

o0 2
3
4 6
5 7

x2 y0
2
Find the shortest path from source
vertex to all other Source
vertexvertex S=0
S=Finally contains vertex of final shortest path
Q= Priority queue used to store vertices based on
1 v0 their distance from source vertex
u0
10

o0 3 Initially
2 4 S= { Ø}
6
Q= o u v x y
5 7 0 ∞ ∞ ∞ ∞
x2 y0
∞ 1 v0 ∞
Initially u0
10
S= { Ø} 0
0 3
2 4 6
Q= o u v x y
0 ∞ ∞ ∞ ∞ 5 7

x2 y0
Q= extract minQueue {Q} ∞
S={o} ∞ 2
∞ 1 v0 ∞
Initially u0
10
S= { Ø} 0
0 3
2 4 6
Q= o u v x y
0 ∞ ∞ ∞ ∞ 5 7

x2 y0
Q= extract minQueue {Q} ∞
S={o} ∞ 2

∞ 1 v0 ∞
u0
10
0
Q= u v x y
0 3
10 ∞ 5 ∞ 2 4 6
Q= extract minQueue {Q} 5 7
S={o, x}
2x y0

5 2
S={o, x} ∞ 1 v0 ∞
u0
10
Q= u v y 0
8 ∞ 7 0 3
2 4 6
Q= extract minQueue {Q}
S={o,x,y} 5 7

2x y0
7
5 2
S={o, x} ∞ 1 v0 ∞
u0
10
Q= u v y 0
8 ∞ 7 0 3
2 4 6
Q= extract minQueue {Q}
S={o,x,y} 5 7

2x y0
7
5 2

Q= u v 8 ∞
1 v0
8 13 u0
Q= extract minQueue {Q} 10
0
S={o, x, y , u }
0 3
2 4 6
5 7

2x y0
7
5 2
8 9
S={o, x, y , u } 1 v0
u0
10
Q= v 0
9 0 3
2 4 6
Q= extract minQueue {Q}
5 7
S={o,x,y, u, v}
2x y0
7
5 2

1 v0
u0

0 3

5
2x y0
2
problem
solution
problem
solution
DIJKSTRA’S ALGO
Step1: Initialize single source vertex by zero.
Step2: Initialize S= {Ø} where S, finally contains vertices of final shortest
path from source vertex.
Step3: Initialize priority queue Q such that Q V(G)
Step 4: while priority queue (Q) is not empty do step 5 to 7
Step5: select vertex u of minimum value from Queue such that
u extract of minQueue
Step6: new S SU {u}
Step7: Insert all the adjacent nodes into Queue,assign their weights as
sum of weight of parent and edge weight.
Step8: End;
Transitive closure of a graph
• Given a directed graph, find out if a vertex j is reachable from another
vertex i for all vertex pairs (i, j) in the given graph. Here reachable
mean that there is a path from vertex i to j. The reach-ability matrix is
called the transitive closure of a graph.
Transitive closure of
graphs is

1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1
Floyd Warshall Algorithm, find the
shortest path distance between
every pair of vertices
Floyd Warshall Algorithm
Step1: We initialize the solution matrix same as the input graph matrix .
Step2: Then we update the solution matrix by considering all vertices as an
intermediate vertex. The idea is to one by one pick all vertices and updates
all shortest paths which include the picked vertex as an intermediate vertex
in the shortest path.
When we pick vertex number k as an intermediate vertex, we already have
considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i,
j) of the source and destination vertices respectively.
There are two possible cases:
Cae1:k is not an intermediate vertex in shortest path from i to j. We keep the
value of dist[i][j] as it is.
Case2: k is an intermediate vertex in shortest path from i to j. We update the
value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]

You might also like