CH10 1
CH10 1
BIBHA STHAPIT
ASST. PROFESSOR
IoE, PULCHOWK CAMPUS
Introduction
• A graph is an abstract data structure which is basically, a collection of
vertices (also called nodes) and edges that connect these vertices.
• A graph is often viewed as a generalization of the tree structure, where
instead of a having a purely parent-to-child relationship between tree
nodes, any kind of complex relationships between the nodes can be
represented.
Chapter 10
• Why graphs are useful?
• Graphs are widely used to model any situation where entities or things
are related to each other in pairs; for example, the following
information can be represented by graphs:
• Family trees in which the member nodes have an edge from parent to
each of their children.
• Transportation networks in which nodes are airports, intersections,
ports, etc. The edges can be airline flights, one-way roads, shipping 2
routes, etc.
Definition
• A graph G is defined as an ordered set (V, E), where V(G) represent
the set of vertices and E(G) represents the edges that connect the
vertices.
Chapter 10
E(G) = { (A, B), (B, C), (A, D), (B, D), (D, E), (C, E) }. Note that there are
5 vertices or nodes and 6 edges in the graph.
A B C
D E
3
Definition
• A graph can be directed or undirected. In an undirected graph, the
edges do not have any direction associated with them. That is, if an
edge is drawn between nodes A and B, then the nodes can be
traversed from A to B as well as from B to A. The above figure shows
an undirected graph because it does not gives any information about
Chapter 10
the direction of the edges.
D E
Graph terminology
• Adjacent Nodes or Neighbors: For every edge, e = (u, v) that
connects nodes u and v; the nodes u and v are the end-points and
are said to be the adjacent nodes or neighbors.
• Degree of a node: Degree of a node u, deg(u), is the total number of
edges containing the node u. If deg(u) = 0, it means that u does not
Chapter 10
belong to any edge and such a node is known as an isolated node.
• Regular graph: Regular graph is a graph where each vertex has the
same number of neighbors. That is every node has the same degree.
A regular graph with vertices of degree k is called a k-regular graph
or regular graph of degree k.
5
Graph terminology
• Path: A path P, written as P = {v0, v1, v2,….., vn), of length n from a node u
to v is defined as a sequence of (n+1) nodes. Here, u = v0, v = vn and vi-1 is
adjacent to vi for i = 1, 2, 3, …, n.
• Closed path: A path P is known as a closed path if the edge has the same
end-points. That is, if v0 = vn.
• Simple path: A path P is known as a simple path if all the nodes in the path
Chapter 10
are distinct with an exception that v0 may be equal to vn. If v0 = vn, then
the path is called a closed simple path.
• Cycle: A closed simple path with length 3 or more is known as a cycle. A
cycle of length k is called a k – cycle.
• Connected graph: A graph in which there exists a path between any two of
its nodes is called a connected graph. That is to say that there are no
isolated nodes in a connected graph. A connected graph that does not have
any cycle is called a tree.
• Complete graph: A graph G is said to be a complete, if all its nodes are fully 6
connected, that is, there is a path from one node to every other node in the
graph. A complete graph has n(n-1)/2 edges, where n is the number of
nodes in G.
Graph terminology
• Labeled graph or weighted graph: A graph is said to be labeled if
every edge in the graph is assigned some data. In a weighted graph,
the edges of the graph are assigned some weight or length. Weight
of the edge, denoted by w(e) is a positive value which indicates the
cost of traversing the edge.
Chapter 10
• Multiple edges: Distinct edges which connect the same end points
are called multiple edges. That is, e = {u, v) and e’ = (u, v) are known
as multiple edges of G.
• Loop: An edge that has identical end-points is called a loop. That is,
e = (u, u).
• Multi- graph: A graph with multiple edges and/or a loop is called a
multi-graph.
• Size of the graph: The size of a graph is the total number of edges in
7
it.
Graph terminology
3 4
Chapter 10
A B C
e1 A B C
A B e4
2
e2 7 1
e3 D E F
C e6 D B
e5 e7 3
8
Adjacency matrix representation
• An adjacency matrix is used to represent which nodes are
adjacent to one another. By definition, we have learnt that,
two nodes are said to be adjacent if there is an edge
connecting them.
• In a directed graph G, if node v is adjacent to node u, then
Chapter 10
surely there is an edge from u to v. That is, if v is adjacent to u,
we can get from u to v by traversing one edge. For any graph G
having n nodes, the adjacency matrix will have dimensions of
n X n.
• In an adjacency matrix, the rows and columns are labeled by
graph vertices. An entry aij in the adjacency matrix will contain
1, if vertices vi and vj are adjacent to each other. However, if
the nodes are not adjacent, aij will be set to zero. 9
1 if vi is adjacent to vj, that is there is an edge (vi, vj)
aij
0 otherwise
Adjacency matrix representation
• Since an adjacency matrix contains only 0s and 1s, it is called a bit
matrix or a Boolean matrix. The entries in the matrix depend on the
ordering of the nodes in G. therefore, a change in the order of nodes
will result in a different adjacency matrix.
Chapter 10
10
Adjacency list representation
•The adjacency list is another way in which graphs can be represented in
computer’s memory. This structure consists of a list of all nodes in
G. Furthermore, every node is in turn linked to its own list that contains the
names of all other nodes that are adjacent to itself.
• The key advantage of using an adjacency list includes:
Chapter 10
• It is easy to follow, and clearly shows the adjacent nodes of a particular
node
• It is often used for storing graphs that have a small to moderate number of
edges. That is an Adjacency list is preferred for representing sparse graphs in
computer’s memory; otherwise, an adjacency matrix is a good choice.
• Adding new nodes in G is easy and straightforward when G is
represented using an Adjacency list. Adding new nodes in an Adjacency 11
matrix is a difficult task as size of the matrix needs to be changed and
existing nodes may have to be reordered.
Adjacency list representation
• For a directed graph, the sum of lengths of all adjacency lists is
equal to the number of edges in G. However, for an undirected
graph, the sum of lengths of all adjacency lists is equal to
twice the number of edges in G because an edge (u, v) means
an edge from node u to v as well as an edge v to u. The
Chapter 10
adjacency list can also be modified to store weighted graphs.
B D X
B
C C D X
D
D
12
A B X
Adjacency list representation
A B D X
A B C
B A C D X
D E C B E X
Chapter 10
D A B E X
E D X
C
13
Graph traversal
• By traversing a graph, we mean the method of examining the nodes
and edges of the graph. There are two standard methods of graph
traversal which we will discuss in this section. These two methods
are-
• Breadth first search
Chapter 10
• Depth first search
• While breadth first search will use a queue as an auxiliary data
structure to store nodes for further processing, the depth-first
search scheme will use a stack. But both these algorithms will make
use of a variable STATUS. During the execution of the algorithm,
every node in the graph will have the variable STATUS set to 1, 2 or
depending on its current state. Table I shows the value of status and
its significance. 14
Graph traversal
STATE OF THE
STATUS DESCRIPTION
NODE
Chapter 10
Node N is placed on the queue or stack
2 Waiting
and waiting to be processed
15
Breadth first traversal/search
• Breadth-first search (BFS) is a graph search algorithm that begins at
the root node and explores all the neighboring nodes. Then for each
of those nearest nodes, the algorithm explores their unexplored
neighbor nodes, and so on, until it finds the goal.
Chapter 10
• That is, we start examining the node A and then all the neighbors of
A are examined. In the next step we examine the neighbors of
neighbors of A, so on and so forth
16
Breadth first traversal/search
Algorithm for breadth-first search in a graph G beginning at a starting node A
Chapter 10
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
• Step 5: Enqueue all the neighbors of N that are in the ready state
(whose STATUS = 1) and set their STATUS = 2 (waiting state)
• [END OF LOOP]
• Step 6: EXIT
17
Breadth first search
• Example: Consider the graph G given below. The
adjacency list of G is also given.
Chapter 10
A
Adjacency Lists
A: B, C, D
B: E
B C D C: B, G
D: C, G
E: C, F
F: C, H
E F G G: F, H, I
H: E, I
I: F 18
H I
Chapter 10
A, B, C D, E, G
A, B, C, D E, G
A, B, C, D, E G, F
A, B, C, D, E, G F, H
A, B, C, D, E, G, F H
A, B, C, D, E, G, F, H I
A, B, C, D, E, G, F, H, I NULL
19
Breadth first traversal
A A X G H P E M Y J
X H Y
Chapter 10
E
G P M J
A X GH HP PE E MY YJ J
20
queue
Depth first search/traversal
• The Depth First Search algorithm progresses by expanding the
starting node of G and thus going deeper and deeper until a goal
node is found, or until a node that has no children is
encountered. When a dead- end is reached, the algorithm
backtracks, returning to the most recent node that has not been
completely explored.
Chapter 10
• In other words, the Depth- First Search algorithm begins at a
starting node A which becomes the current node. Then it
examines each node N along a path P which begins at A. That is,
we process a neighbor of A, then a neighbor of neighbor of A
and so on. During the execution of the algorithm, if we reach a
path that has a node N that has already been processed, then 21
Chapter 10
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4:Pop the top node N. Process it and set its
STATUS = 3 (processed state).
Step 5:Push on to the stack all the neighbors of N that
are in the ready state (whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
22
Depth first search
• Example: Consider the graph G given below. The
adjacency list of G is also given. Suppose we want to
print all nodes that can be reached from the node A
(including A itself).
Chapter 10
A
Adjacency Lists
A: B, C, D
B: E
C: B, G
D: C, G
B C D
E: C, F
F: C, H
G: F, H, I
H: E, I
I: F
E F G
23
H I
Set of visited vertices Stack
NULL A
A B, C, D
A, D B, C, G
Chapter 10
A, D, G B, C, F, H, I
A, D, G , I B, C, F, H
A, D, G , I, H B, C, F, E
A, D, G , I, H, E B, C, F
A, D, G , I, H, E, F B, C
A, D, G , I, H, E, F, C B
A, D, G , I, H, E, F, C, B NULL
24
Depth first traversal
A A X H P E Y M J G
X H Y
Chapter 10
E
G P M J
P Y
H E E M M J
A X G G G G G G G 25
stack
Minimum spanning tree (MST)
• A spanning tree of a connected, undirected graph G, is a
sub-graph of G which is a tree that connects all the vertices
together. A graph G can have many different spanning
trees.
• When we assign weights to each edge (which is a number
Chapter 10
that represents how unfavorable the edge is), and use it to
assign a weight to a spanning tree by calculating the sum of
the weights of the edges in that spanning tree.
• A minimum spanning tree (MST) is defined as a spanning
tree with weight less than or equal to the weight of every
other spanning tree. In other words, a minimum spanning
tree is a spanning tree that has weights associated with its 26
edges, and the total weight of the tree (the sum of the
weights of its edges) is at a minimum.
Minimum spanning tree
Example: Consider an un-weighted graph G given below.
From G we can draw many distinct spanning trees. (Eight
of them are given here). For an un-weighted graph, every
spanning tree is a minimum spanning tree.
Chapter 10
A B A B A B A B A B
A B A B
C D C D C D C D C D
C D C D
Unweighted Graph
A B
A B A B
C D
C D C D 27
Minimum spanning tree
• Example: Consider a weighted graph G given below. From
G we can draw three distinct spanning trees. But only a
single minimum spanning tree can be obtained, that is,
the one that has minimum weight (cost) associated with
Chapter 10
it.
28
Minimum spanning tree
• Thus, we see that of all the given spanning trees, the tree
given below is called the minimum spanning tree as it
has the lowest cost associated with it.
Chapter 10
29
Prim’s algorithm for MST
Tree vertices: Vertices that are a part of the minimum spanning
tree T.
Fringe vertices: Vertices that are currently not a part of T, but
are adjacent to some tree vertex.
Chapter 10
Unseen Vertices: Vertices that are neither tree vertices nor
fringe vertices fall under this category.
The steps in the Prim algorithm can be given as follows:
• Choose a starting vertex
• Branch out from the starting vertex and during each iteration
select a new vertex and edge. Basically, during each iteration
of the algorithm, we have to select a vertex from the fringe
vertices in such a way that the edge connecting the tree vertex 30
and the new vertex has minimum weight assigned to it.
Prim’s algorithm for MST
Prim’s Algorithm
Chapter 10
Step 3:Select an edge e connecting the tree
vertex and fringe vertex that has
minimum weight
Step 4:Add the selected edge and the vertex to
the minimum spanning tree T
[END OF LOOP]
Step 5: EXIT
31
Prim’s algorithm for MST
Example: Construct a minimum spanning tree of the graph
given below. A
7
B
11
E
4
3 9
C D
Chapter 10
10
Chapter 10
7
vertex. A B
Chapter 10
4
3
C D
10
A B E
3
4
9
34
C D
10
Prim’s algorithm for MST
• Select an edge connecting the tree vertex and fringe vertex that has
minimum weight and add the selected edge and the vertex to the
minimum spanning tree T. Since the edge connecting B and D has
less weight, so add D to the tree. Now D is not a fringe vertex but a
tree vertex. 11
A B E
Chapter 10
4
3 9
C D
A B E
4 35
3 9
C D
5 5
B D 6 B D 3
6 3
2 3 2 F A 2 3 2 F
A
5 3 5
3 C E C E
4 4
5 5
B D 6 B D 3
6 3
2 3 2 F A 2 3 2 F
A
5 3 5
3 C E C E
4 4
5 5
B D 6 B D 3
6 3
2 3 2 F A 2 3 2 F
A 36
5 3 5
3 C E C E
4 4
Chapter 10
37
Given graph
Kruskal’s algorithm for MST
• Like the Prim’s algorithm, the Kruskal's algorithm is used to
find a minimum spanning tree for a connected weighted
graph. That is, the algorithm aims to find a subset of the edges
that forms a tree that includes every vertex. The total weight
of all the edges in the tree is minimized. However, if initially,
Chapter 10
the graph is not connected then it finds a minimum spanning
forest (Note that a forest is a collection of trees. Similarly, a
minimum spanning forest is a collection of minimum spanning
trees).
Chapter 10
Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
Step 4: Remove an edge from Q
Step 5: IF the edge obtained in Step 4 connects two
different trees, then ,
Add it to the forest (for combining two
trees into one tree).
ELSE
Discard the edge
Step 6: END
39
Kruskal’s algorithm for MST
Example: Apply Kruskal’s algorithm on the graph given below
7 A
1
6
D
B 8 C 5
5
Chapter 10
4
3
E F
2
Chapter 10
5 {{A, C, D, E, F}, {B}} {(A, D), (C, E), {(D, F), (A, C), (A, B), (B, C)}
(E, F), (E, D)}
6 {{A, C, D, E, F}, {B}} {(A, D), (C, E), {(A, C), (A, B), (B, C)}
(E, F), (E, D)}
7 {{A, C, D, E, F}, {B}} {(A, D), (C, E), {(A, B), (B, C)}
(E, F), (E, D)}
8 {A ,B, C, D, E, F} {(A,D),(C,E),(E,F) {(B, C)}
, (E,D), (A, B)}
9 {A ,B, C, D, E, F} {(A,D),(C,E),(E,F) ---- 41
, (E,D), (A, B)}
• The MST is obtained as,
7 A
Chapter 10
B
C
3 4
E F
42
Dijkstra’s algorithm
• Dijkstra's algorithm, given by a Dutch scientist Edsger
Dijkstra in 1959, is used to find the shortest path tree.
• Given a graph G and a source node A, the algorithm is
used to find the shortest path (one having the lowest
Chapter 10
cost) between A (source node) and every other node.
• Moreover, Dijkstra’s algorithm is also used for finding
costs of shortest paths from a source node to a
destination node.
• That means, it solves single source shortest path problem
for a directed graph G(V,E) with non-negative edge
weights i.e. w(u,v)>=0 43
Dijkstra’s algorithm
Dijkastra’s Algorithm
1. Select the source node also called the initial node
2. Define an empty set N that will be used to hold nodes
to which a shortest path has been found.
3. Label the initial node with 0, and insert it into N.
4. Repeat Steps 5 to 7 until the destination node is in N
or there are no more labeled nodes in N.
Chapter 10
5. Consider each node that is not in N and is connected
by an edge from the newly inserted node.
6. a. If the node that is not in N has no labeled
then SET the label of the node = the label of the
newly inserted node + the length of the edge.
b. Else if the node that is not in N was already
labeled, then SET its new label = minimum (label of
newly inserted vertex + length of edge, old label)
7. Pick a node not in N that has the smallest label 44
assigned to it and add it to N
Dijkstra’s algorithm
• Dijkstra's algorithm labels every node in the graph where the
labels represent the distance (cost) from the source node to
that node. There are two kinds of labels: temporary and
permanent. While temporary labels are assigned to nodes that
have not been reached, permanent labels are given to nodes
Chapter 10
that have been reached and their distance (cost) to the source
node is known.
• The execution of this algorithm will produce either of two
results-
• If the destination node is labeled, then the label will in turn
represent the distance from the source node to the destination
node. 45
• If it the destination node is not labeled, then it means that there is
no path from the source to the destination node.
Dijkstra’s algorithm
• Example: Consider the graph G given below. Taking D as the initial
node, execute the Dijkastra’s algorithm on it.
4
A B
17
Chapter 10
2 15
C D E
5 23
9 11
G
F
13
46
Dijkstra’s algorithm
• Step 1: SET the label of D = 0 and N = {D}.
• Step 2: Label of D = 0, B = 15, G = 23 and F = 5. Therefore, N =
{D, F}.
• Step 3: Label of D = 0, B = 15, G has been re-labeled 18 because
minimum (5 + 13, 23) = 18, C has been re-labeled 14 (5 + 9).
Chapter 10
Therefore, N = {D, F, C}.
• Step 4: Label of D = 0, B = 15, G = 18. Therefore, N = {D, F, C, B}.
• Step 5: Label of D = 0, G = 18 and A = 19 (15 + 4). Therefore, N =
{D, F, C, B, G}.
• Step 6: Label of D = 0 and A = 19. Therefore,N = {D, F, C, B, G, A}
• Note that we have no label for node E, this means that E is not 47
reachable from D. only the nodes that are in N are reachable
from B.
Warshall’s algorithm
• Computes the transitive closure of a relation (Alternatively: all
paths in a directed graph)
• Warshall’s algorithm defines matrices P0, P1, P2, …. Pn as
given as
0 otherwise
Chapter 10
Pk[i][j]
1 if there is a path from vi to vj. The path should not use any other
nodes except v1, v2, …., vk
48
Warshall’s algorithm
• This means, if P0[i][j] = 1, then there exists an edge from node vi to vj.
• If P1[i][j] = 1, then there exists an edge from vi to vj that does not use
any other vertex except v1.
• If P2[i][j] = 1, then there exists an edge from vi to vj that does not use
Chapter 10
any other vertex except v1 and v2.
• Note P0 is equal to the adjacency matrix of G. If G contains n nodes
then Pn = P which is the path matrix of the graph G.
49
Warshall’s algorithm
• Therefore, we can conclude that, Pk[i][j] is equal to 1 only when
either of the two cases occur:
• There is a path from vi to vj that does not use any other node except v1,
v2, …, vk-1. Therefore, Pk-1[i][j] = 1.
• There is a path from vi to vk and a path from a path from vk to vj
Chapter 10
where every edge does not use any other node except v1, v2, …,
vk-1. Therefore,
• Pk-1[i][k] = 1 AND Pk-1[k][j] = 1
• Hence, the path matrix Pn can be calculated with the formula given
as,
• Pk[i][j] = Pk-1[i][j] V (Pk-1[i][k] Λ Pk-1[k][j]) 50
Warshall’s algorithm
Warshall’s algorithm to find path matrix P
Chapter 10
Step 3: IF A[I][J] = 0, then SET P[I][J] = 0
ELSE P[I][J] = 1
[END OF LOOP]
[END OF LOOP]
Step 4: [Calculate the path matrix P] Repeat
Step 5 for K = 0 to n-1
Step 5: Repeat Step 6 for I = 0 to n-1
Step 6: Repeat Step 7 for J=0 to n-1
Step7: SET PK[I][J] = PK-1[I][J] V (PK-1[I][K]
Λ PK-1[K][J]) 51
Step 8: EXIT
Chapter 10
52
Floyd -Warshall’s algorithm
• The modified Warshall’s algorithm is used to obtain a matrix that
gives the shortest paths between the every nodes in weighted
graph G with positive and negative edges.
• As an input to the algorithm, we will take the Adjacency matrix A
of G and replace all those values of A which are zero by infinity
(∞). Infinity (∞) denotes a very large number and indicates that
Chapter 10
there no path between the vertices.
• In Warshall’s modified algorithm, we will obtain a set of matrices
Q0, Q1, Q2,…., Qm using the formula given below.
Qk[i][j] = Minimum( Mk-1[i][j], Mk-1[i][k] + Mk-1[k][j])
• Q0 is exactly the same as A with a little difference that every
element that has a zero value in A is replaced by (∞) in Q0. Using 53
the formula given above the matrix Qn will give the path matrix
that has the shortest path between the vertices of the graph.
Floyd - Warshall’s algorithm
Floyd-Warshall’s algorithm to find the shortest path matrix P
Chapter 10
[END OF LOOP]
[END OF LOOP]
Step 4: [Calculate the shortest path matrix Q] Repeat Step 5 for
K = 0 to n-1
Step 5: Repeat Step 6 for I = 0 to n-1
Step 6: Repeat Step 7 for J=0 to n-1
Step7: IF Q[I][J] <= Q[I][K] + Q[K][J]
SET Q[I][J] = Q[I][J]
ELSE SET Q[I][J] = Q[I][K] + Q[K][J]
[END OF IF]
[END OF LOOP] 54
[END OF LOOP]
[END OF LOOP]
Step 8: EXIT
Floyd - Warshall’s algorithm
Chapter 10
55
Chapter 10
THANK YOU!
56