You are on page 1of 50

DSA with Java/Unit- 7

Unit 7: Graphs
 A graph is a collection of vertices (or nodes) and the connections (edges) between them.
Generally, no restriction is imposed on the number of vertices in the graph or on the number
of connections one vertex can have to other vertices.
 A simple graph G = (V, E) consists of a nonempty set V of vertices and a possibly empty
set E of edges, each edge being a set of two vertices from V. The number of vertices and
edges is denoted by |V| and |E |, respectively.
 Graphs are versatile data structures that can represent a large number of different situations
and events from diverse domains.
Directed edge
 ordered pair of vertices (u, v)
 first vertex u is the origin second vertex v is the destination
 (u,v) ≠ (v,u)

Undirected edge:
 unordered pair of vertices (u, v)
 (u,v) = (v,u)

Directed graph
 All the edges are directed
 Also called as digraph

1
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Undirected graph
 All the edges are undirected

 When an edge connects two vertices, the vertices are said to be adjacent to each other and the
edge is incident on both vertices.

 The degree of a vertex is the number of edges incident on it.

 If deg(v) = 0, then v is called an isolated


 A path in a graph is a sequence of adjacent vertices and the vertices are different.
 A sequence of adjacent vertices v1,v2,v3,…, vn is called circuit If v1 = vn .
 A cycle is a path where the first and last vertices are the same.
 Cycle is the closed path
 If a vertices in circuit are different then it is cycle.

Path : a,b,c,d,e
Circuit: a,b,c,d,e,b,a
Cycle: a,b,c,d,e,a
 A graph is connected if there is a path from every vertex to every other vertex. In connected
graph, at least one path exists between every pair of vertices. Example:

2
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 A graph in which there does not exist any path between at least one pair of vertices is
called as a disconnected graph. Example:

 A graph is called a weighted graph if each edge has an assigned number. Depending on
the context in which such graphs are used, the number assigned to an edge is called its
weight, cost, distance, length, or some other name

 A graph with n vertices is called complete and is denoted Kn if for each pair of distinct
vertices there is exactly one edge connecting them; that is, each vertex can be connected to
any other vertex

3
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 A (self ) loop is an edge that connects a vertex to itself.(edge that has same endpoints)
 Two edges are parallel if they connect the same pair of vertices
 A multigraph is a graph in which two vertices can be joined by multiple edges.
 Contains some parallel edges but no self- loops.

 A pseudo graph is graph which contains parallel edges and self-loops.

Graph Representation
In graph theory, a graph representation is a technique to store graph into the memory of computer.
There are various ways to represent a graph. Some of them are :
 Adjacency list
 Adjacency matrix
 Incident matrix
Adjacency List Representation
 Adjacency List specifies all vertices adjacent to each vertex of the graph.
 Every vertex of the graph contains list of its adjacent vertices.
 Each node is holding a list of nodes, which are directly connected with that vertices.

4
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 This list can be implemented as a table, in which case it is called a star representation
(Figure 1b).
 The list can also be implemented as linked list.(Figure 1c)
Example:

Figure 1: Adjacency list representation of a graph

5
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Adjacency matrix
 An adjacency matrix of graph G = (V,E) is a binary |V| × |V| matrix such that each entry of
this matrix

Example:

Figure 2: Adjacency matrix representation of a graph

6
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Incident matrix
An incidence matrix of graph G = (V, E) is a |V| × |E| matrix such that

Example:

Figure 3: Incidence matrix representation of graph

Graph Traversals
 Process of visiting every node of a graph exactly once.
 Two popular graph traversal techniques :
o DFS (Depth First Search) Traversal
o BFS (Breadth First Search) Traversal

7
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Depth First Search (DFS) Algorithm


The DFS algorithm works as follows:
1. Create a stack STK to store the vertices.
2. Push the source vertex S in the stack ‘STK’.
3. Mark S as visited. (i.e. add S to the visited list)
4. While the stack STK is not empty
4. Pop the vertex U from the top of the stack. i.e Vertex U = STK.top(), STK.pop()
5. If the vertex U is not visited
6. Explore the vertex U and mark U as visited (i.e. add U to .Visited List)
7. For every vertex V adjacent to vertex U
8. Push the vertex V in the stack STK

Example:

Let 0 be the starting node or source node. First, we mark it as visited and add it to the visited
list. Then we push all its adjacent nodes in the stack.

8
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Next, we take one of the adjacent nodes to process i.e. the top of the stack which is 1. We mark it
as visited by adding it to the visited list. Now look for the adjacent nodes of 1. As 0 is already in
the visited list, we ignore it and we visit 2 which is the top of the stack.

Next, we mark node 2 as visited. Its adjacent node 4 is added to the stack.

Next, we mark 4 which is the top of the stack as visited. Node 4 has only node 2 as its adjacent
which is already visited, hence we ignore it.

At this stage, only node 3 is present in the stack. Its adjacent node 0 is already visited, hence we
ignore it. Now we mark 3 as visited.

9
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Now the stack is empty and the visited list shows the sequence of the depth-first traversal of the
given graph.

Try Yourself: Trace for DFS graph traversal

Breadth First Search (BFS) Algorithm


The BFS algorithm works as follows:
1. Create a queue Q to store the vertices.
2. Enqueue the source vertex S to the queue Q.
3. Mark S as visited. (i.e. add S to the visited list)
4. While the queue Q is not empty
5. Dequeue the vertex U from the front of the queue. i.e Vertex U = Q.front(), Q.Dequeue()
(i.e. Remove U from queue)
6. If the vertex U is not visited
7. Explore the vertex U and mark U as visited.( i.e. add U to visited list)
8. For every vertex V adjacent to the vertex U
9. Enqueue the vertex V in to queue Q.

10
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Example:

Let 0 be the starting node or source node. First, we add to the visited list and all its adjacent nodes
in the queue.

Next, we take one of the adjacent nodes to process i.e. 1. We mark it as visited by removing it
from the queue and put its adjacent nodes in the queue (2 and 3 already in queue). As 0 is already
visited, we ignore it.

Next, we dequeue node 2 and mark it as visited. Then, its adjacent node 4 is added to the queue.

11
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Next, we dequeue 3 from the queue and mark it as visited. Node 3 has only one adjacent node i.e.
0 which is already visited. Hence, we ignore it.

At this stage, only node 4 is present in the queue. Its adjacent node 2 is already visited, hence we
ignore it. Now we mark 4 as visited.

Next, the sequence present in the visited list is the breadth-first traversal of the given graph.

12
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Shortest Paths
 Finding the shortest path is a classical problem in graph theory, and a large number of
different solutions have been proposed.
 Edges are assigned certain weights representing, for example, distances between cities,
times separating the execution of certain tasks, costs of transmitting information between
locations, amounts of some substance transported from one place to another, and so on.
 When determining the shortest path from vertex v to vertex u, information about distances
between intermediate vertices w has to be recorded. This information can be recorded as a
label associated with these vertices, where the label is only the distance from v to w or the
distance along with the predecessor of w in this path.
 The methods of finding the shortest path rely on these labels.
 Depending on how many times these labels are updated, the methods solving the shortest
path problem are divided in two classes: label-setting methods and label-correcting
methods.
 For label-setting methods, in each pass through the vertices still to be processed, one
vertex is set to a value that remains unchanged to the end of the execution. This, however,
limits such methods to processing graphs with only positive weights.
 The second category includes label-correcting methods, which allow for the changing of
any label during application of the method. The latter methods can be applied to graphs
with negative weights and with no negative cycle—a cycle composed of edges with
weights adding up to a negative number—but they guarantee that, for all vertices, the
current distances indicate the shortest path only after the processing of the graph is finished.
 Most of the label-setting and label-correcting methods, however, can be subsumed to the
same form, which allows finding the shortest paths from one vertex to all other vertices

13
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Generic Shortest Path Algorithm

 In this generic algorithm, a label consists of two elements:


label(v) = (currDist(v), predecessor(v))
 This algorithm leaves two things open: the organization of the set toBeChecked and the
order of assigning new values to v in the assignment statement
v = a vertex in toBeChecked;
 It should be clear that the organization of toBeChecked can determine the order of choosing
new values for v, but it also determines the efficiency of the algorithm.
 What distinguishes label-setting methods from label-correcting methods is the method of
choosing the value for v, which is always a vertex in toBeChecked with the smallest current
distance.
 One of the first label-setting algorithms was developed by Dijkstra.
Dijkstra’s algorithm

 In Dijkstra’s algorithm, a number of paths p1, . . . , pn from a vertex v are tried, and each
time, the shortest path is chosen among them, which may mean that the same path pi can
be continued by adding one more edge to it.
 But if pi turns out to be longer than any other path that can be tried, pi is abandoned and
this other path is tried by resuming from where it was left and by adding one more edge to
it.

14
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 Because paths can lead to vertices with more than one outgoing edge, new paths for
possible exploration are added for each outgoing edge. Each vertex is tried once, all paths
leading from it are opened, and the vertex itself is put away and not used anymore. After
all vertices are visited, the algorithm is finished.

Dijkstra’s algorithm is obtained from the generic method by being more specific about which
vertex is to be taken from toBeChecked so that the line
v = a vertex in toBeChecked;
is replaced by the line
v = a vertex in toBeChecked with minimal currDist(v);

Dijkstra’s Algorithm is a very famous greedy algorithm. It is used for solving the single source
shortest path problem. It computes the shortest path from one particular source node to all other
remaining nodes of the graph.
Dijkstra’s algorithm is not general enough in that it may fail when negative weights are used in
graphs.

15
Collected by Bipin Timalsina
DSA with Java/Unit- 7

It is important to note the following points regarding Dijkstra Algorithm-


 Dijkstra algorithm works only for connected graphs.
 Dijkstra algorithm works only for those graphs that do not contain any negative weight
edge.
 Dijkstra algorithm works for directed as well as undirected graphs.

Example: An execution of DijkstraAlgorithm().

 The list toBeChecked is initialized to {a b . . . j}; the current distances of all vertices are
initialized to a very large value, marked here as ∞; and in the first iteration, the current
distances of d’s neighbors are set to numbers equal to the weights of the edges from d.

16
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 Now, there are two candidates for the next try, a and h, because d was excluded from
toBeChecked.
 In the second iteration, h is chosen, because its current distance is minimal, and then the
two vertices accessible from h, namely, e and i, acquire the current distances 6 and 10.
 Now, there are three candidates in toBeChecked for the next try, a, e, and i. a has the
smallest current distance, so it is chosen in the third iteration. Eventually, in the tenth
iteration, toBeChecked becomes empty and the execution of the algorithm completes
Ford Algorithm
 One of the first label-correcting algorithms was devised by Lester Ford. Like Dijkstra’s
algorithm, it uses the same method of setting current distances, but Ford’s method does not
permanently determine the shortest distance for any vertex until it processes the entire
graph.
 It is more powerful than Dijkstra’s method in that it can process graphs with negative
weights (but not graphs with negative cycles).

 To impose a certain order on monitoring the edges, an alphabetically ordered sequence of


edges can be used so that the algorithm can repeatedly go through the entire sequence and
adjust the current distance of any vertex, if needed
Example: FordAlgorithm() applied to a digraph with negative weights

The graph includes edges with negative weights. The table indicates iterations of the while loop
and current distances updated in each iteration, where one iteration is defined as one pass through
the edges. Note that a vertex can change its current distance during the same iteration. However,
at the end, each vertex of the graph can be reached through the shortest path from the starting
vertex (vertex c in this example).

17
Collected by Bipin Timalsina
DSA with Java/Unit- 7

All-to-All Shortest Path Problem

Although the task of finding all shortest paths from any vertex to any other vertex seems to be
more complicated than the task of dealing with one source only, a method designed by Stephen
Warshall and implemented by Robert W. Floyd and P. Z. Ingerman does it in a surprisingly simple
way, provided an adjacency matrix is given that indicates all the edge weights of the graph (or
digraph). The algorithm is also known as Floyd Warshall algorithm or WFI algorithm.
Note: The graph can include negative weights.

18
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Floyd Warshall algorithm or WFI algorithm


Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of
vertices in a weighted graph. This algorithm works for both the directed and undirected weighted
graphs. But, it does not work for the graphs with negative cycles (where the sum of the edges in a
cycle is negative).
Floyd-Warhshall algorithm is also called as Floyd's algorithm, Roy-Floyd algorithm, Roy-
Warshall algorithm, or WFI algorithm.

The outermost loop refers to vertices that may be on a path between the vertex with index j and
the vertex with index k. For example, in the first iteration, when i = 1, all paths vj . . . v1 . . . vk are
considered, and if there is currently no path from vj to vk and vk is reachable from vj, the path is
established, with its weight equal to p = weight(path(vjv1)) + weight(path(v1vk)), or the current
weight of this path, weight(path(vjvk)), is changed to p if p is less than weight(path(vjvk)).
Follow the steps below to find the shortest path between all the pairs of vertices.

19
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 Create a matrix A0 of dimension n×n where n is the number of vertices. The row and the
column are indexed as i and j respectively. i and j are the vertices of the graph.
 Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex.
 If there is no path from ith vertex to jth vertex, the cell is left as infinity.

 Now, create a matrix A1 using matrix A0. The elements in the first column and the first row
are left as they are. The remaining cells are filled in the following way.
 Let k be the intermediate vertex in the shortest path from source to
destination. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).

20
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 In this step, k is vertex 1. We calculate the distance from source vertex to destination
vertex through this vertex k.
 Calculate the distance from the source vertex to destination vertex through this vertex
k (here k is 1)

For A1[2, 4], the direct distance from vertex 2 to 4 is infinity and the sum of the distance
from vertex 2 to 4 through vertex (i.e. from vertex 2 to 1 and from vertex 1 to 4) is 15.
Since 15 < infinity , A1[2, 4] is filled with 15.

 Similarly, A2 is created using A1. The elements in the second column and the second row are
left as they are.
 In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as
in step 2.
 Calculate the distance from the source vertex to destination vertex through this vertex 2

21
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 Similarly, A3 and A4 are also created


 Calculate the distance from the source vertex to destination vertex through this vertex 3

 Calculate the distance from the source vertex to destination vertex through this vertex 4

22
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 A4 gives the shortest path between each pair of vertices.


NOTE: While Creating Matrix Ak from Ak-1
 kth row and kth column remains same as in Ak-1
 if (A k-1 [i][j] > (A k-1 [i][k] + A k-1 [k][j]))
Ak [i][j] = A k-1 [i][k] + A k-1 [k][j]
else
Ak [i][j] = A k-1 [i][j]
i.e. Ak [i][j] = min { Ak-1 [i][j] , (A k-1 [i][k] + A k-1 [k][j])}
 This algorithm also allows us to detect cycles if the diagonal is initialized to ∞ and not to
zero.

Cycle Detection
 Floyd Warshall’s algorithm also allows us to detect cycles if the diagonal is initialized to
∞ and not to zero. If any of the diagonal values are changed, then the graph contains a
cycle.
 Cycle in a graph can be detected using DFS

23
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Cycle Detection using DFS


 DFS traversal can be used to determine whether a graph contains cycle or not
 In DFS traversal after popping an element from the stack the element is checked in the
visited list, if the element is already in the stack then there is cycle in the graph.
The DFS Cycle Detection algorithm works as follows:
1. Create a stack STK to store the vertices.
2. Push the source vertex S in the stack ‘STK’.
3. Mark S as visited. (i.e. add S to the visited list)
4. While the stack STK is not empty
4. Pop the vertex U from the top of the stack. i.e Vertex U = STK.top(), STK.pop()
5. If the vertex U is not visited:
6. Explore the vertex U and mark U as visited (i.e. add U to .Visited List)
7. For every vertex V adjacent to vertex U
8. Push the vertex V in the stack STK
5. Else U is already in visited list. Hence there is cycle in the graph.
6. Otherwise there is no cycle in the graph.

Example:

24
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Assume A as a source vertex. A is pushed to stack.


Stack
A

Visited List

A is popped and stored in visited list since A is not in Visited list. Here an adjacent node of A is
B so, B is pushed to stack
Stack
B

Visited List
A

B is now popped which is not in visited list so add B to visited list. C is adjacent node of B which
is now added to stack
Stack
C

Visited List
A B

C is now popped which is not in visited list so add C to visited list. E is adjacent node of C which
is now added to stack
Stack
E

Visited List
A B C

25
Collected by Bipin Timalsina
DSA with Java/Unit- 7

E is now popped which is not in visited list so add E to visited list. D and F are adjacent node of
which are now added to stack
Stack
F D

Visited List
A B C E

F is now popped which is not in visited list so add F to visited list. F does not have adjacent nodes
so no nodes are pushed to stack.
Stack
D

Visited List
A B C E F

D is now popped which is not in visited list so add D to visited list. B is adjacent nodes of D so B
is pushed to stack.
Stack
B

Visited List
A B C E F D

B is popped now. B is already in visited list so there is Cycle in the graph.

26
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Spanning Trees
 The Spanning tree of a graph is a subgraph that contains all the vertices connected by
minimum number of edges and is also a tree.
 A spanning tree does not have any cycle
 A graph can have more than one spanning tree.
 A spanning tree has n −1 edges. Where n is the number of vertices
 From a complete graph, by removing maximum (e - n + 1) edges, we can construct a
spanning tree. Here e is number of edges and n is number of vertices in a graph.
 A complete undirected graph can have nn-2 number of spanning trees where n is the number
of vertices in the graph

 All possible spanning trees of a graph G, have the same number of edges and vertices.
 Removing one edge from the spanning tree will make the graph disconnected
 Adding one edge to the spanning tree will create a circuit or loop
 If all the vertices are connected in a graph, then there exists at least one spanning tree.
 Spanning trees are a subset of connected graph and disconnected graphs do not have
spanning tree

27
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Minimum Spanning Tree (MST)


 A minimum spanning tree is a spanning tree in which the sum of the weights of its edges
is minimal.
 A minimum spanning tree of a graph G is a tree formed from graph edges that connect all
the vertices of G with minimum total cost (weights).
 Popular algorithms to form a MST
o Kruskal’s Algorithm
o Prim’s Algorithm
Kruskal’s Algorithm
 Kruskal’s Algorithm is a famous greedy algorithm.
 Devised by Joseph Kruskal.
 It is used for finding the Minimum Spanning Tree (MST) of a given graph.
 To apply Kruskal’s algorithm, the given graph must be weighted, connected and
undirected.
 In this method, all edges are ordered by weight, and then each edge in this ordered sequence
is checked to see whether it can be considered part of the tree under construction. It is
added to the tree if no cycle arises after its inclusion.
 This algorithm can be summarized as follows:

The main steps in 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.

28
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Example: Constructing a MST from given graph using Kruskal’s Algorithm

The graph contains 5 vertices and 7 edges. So, the minimum spanning tree formed will be having
(5– 1) = 4 edges.
The weights of the edges are:
Edges AE AD AC AB BC CD DE
Weights 5 10 7 1 3 4 2

Edges in ascending order of their weight are:


Edges AB DE BC CD AE AC AD
Weights 1 2 3 4 5 7 10

Start constructing the tree;


Adding AB to the MST (tree) because adding this does not form a cycle;

Now adding DE to the MST; because adding this does not form a cycle

29
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Now Adding BC to the MST

Next, CD is added to MST

4
Now, number of edges in MST has become 4 (i.e. |tree| = |v|-1). Here, all the vertices are included
in MST So we stop here.
. Here the sum of all edge weights in this MST = 1+2+3+4 = 10

30
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Time Complexity of Kruskal’s algorithm : O(|E| lg |E|)


Exercise: What is the cost of MST of given graph? Calculate by using Kruskal’s Algorithm.

Prim’s Algorithm
 Like Kruskal’s algorithm, Prim’s algorithm is also a Greedy algorithm.
 It starts with an empty spanning tree.
 In contrast to Kruskal’s algorithm that starts with graph edges, Prim’s algorithm starts with
a vertex. We start with one vertex and keep on adding edges with the least weight till all
the vertices are covered.
 The idea is to maintain two sets of vertices. The first set contains the vertices already
included in the MST, the other set contains the vertices not yet included.
 At every step, it considers all the edges that connect the two sets, and picks the minimum
weight edge from these edges.
 Prim’s algorithm works by maintaining two sets R and (V-R) where V is the set of vertices
and R is a sub set of V.
 It starts with arbitrary vertex and checks all the edges from that vertex to other vertices
and minimum cost edge is taken at next item to be added in R.
 Initially R is empty and starts with taking a node in R.

31
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Algorithm
PrimsMST(G){
T = ϕ;
R = {vi}; // vi is an arbitrary node from set of all vertices,V
While( R!=V){
choose edge e = (u,v) where u is from R , v is from (V-R) and e is of minimum weight;
add v to R i.e. R = R ∪ {v} and e to T i.e T = T ∪ e
}
return T;
}

The main steps in Prim's algorithm are as follows:


 Choose a random vertex as starting vertex and initialize a minimum spanning tree.
 Find the edges that connect to other vertices. Find the edge with minimum weight and add
it to the spanning tree.
 Repeat step 2 until the spanning tree is obtained.
Example:

Set of all vertices V = {A, B, C, D, E}


Step 1: Let’s choose B as initial vertex
So, R = {B}

32
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Step 2:
R V-R Possible edges with weights (From R Edge with
to V-R) that are not included in MST minimum weight
{B} { A, C, D, E} BC = 10 ; BD = 4 BD

Minimum cost edge BD is added to MST and vertex D is added to R . So, now R = {B, D} and
V-R = { A, C, E}

4
D

Step 3:
R V-R Possible edges with weights (From R to Edge with
V-R) that are not included in MST minimum weight
{ B, D} { A, C, E} BC = 10 ; CD = 2; DE = 1 DE

Minimum cost edge DE is added to MST and vertex E is added to R . So, now R = {B, D, E} and
V-R = { A, C }

33
Collected by Bipin Timalsina
DSA with Java/Unit- 7

B
E
4 1
D

Step 4:
R V-R Possible edges with weights (From R to Edge with
V-R) that are not included in MST minimum weight
{ B, D, E} { A, E} BC = 10 ; CD = 2; CE = 6 CD

Minimum cost edge CD is added to MST and vertex C is added to R . So, now R = {B, D, E, C}
and V-R = { A }
C

B
2 E

4 1
D

Step 5:
R V-R Possible edges with weights (From R to Edge with
V-R) that are not included in MST minimum weight
{ B, D, E, C} {A} BC = 10 ; CE = 6; CA= 3 CA

Minimum cost edge CA is added to MST and vertex A is added to R . So, now
R = {B, D, E, C, A} and V-R = { }
Here V = R so, we stop.

34
Collected by Bipin Timalsina
DSA with Java/Unit- 7

3
C

B 2
E

4 1
D

The cost of MST = 4+ 1+ 2 + 3 = 10

Connectivity
 In many problems, we are interested in finding a path in the graph from one vertex to any
other vertex. For undirected graphs, this means that there are no separate pieces, or
subgraphs, of the graph; for a digraph, it means that there are some places in the graph to
which we can get from some directions but are not necessarily able to return to the starting
points.

Connectivity in Undirected Graphs


 An undirected graph is called connected when there is a path between any two vertices of
the graph.
 Connectivity comes in degrees: A graph can be more or less connected, and it depends on
the number of different paths between its vertices.
 A graph is called n-connected if there are at least n different paths between any two
vertices; that is, there are n paths between any two vertices that have no vertices in common
 .A special type of graph is a 2-connected, or biconnected, graph for which there are at least
two nonoverlapping paths between any two vertices.
 A graph is not biconnected if a vertex can be found that always has to be included in the
path between at least two vertices a and b.
35
Collected by Bipin Timalsina
DSA with Java/Unit- 7

 In other words, if this vertex is removed from the graph (along with incident edges), then
there is no way to find a path from a to b, which means that the graph is split into two
separate subgraphs. Such vertices are called articulation points, or cut-vertices. Vertices
a and b in following figure are examples of articulation points.

 If an edge causes a graph to be split into two subgraphs, it is called a bridge or cut-edge,
as for example, the edge (bc) in above figure.
 Connected subgraphs with no articulation points or bridges are called blocks, or—when
they include at least two vertices—biconnected components

Connectivity in Directed Graphs


 For directed graphs, connectedness can be defined in two ways depending on whether or
not the direction of the edges is taken into account.
 A directed graph is weakly connected if the undirected graph with the same vertices and
the same edges is connected. A directed graph is strongly connected if for each pair of
vertices there is a path between them in both directions.
 The entire digraph is not always strongly connected, but it may be composed of strongly
connected components (SCC), which are defined as subsets of vertices of the graph such
that each of these subsets induces a strongly connected digraph

Strongly Connected Graph

36
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Weakly Connected Graph

Topological Sort
 In many situations, there is a set of tasks to be performed. For some pairs of tasks, it matters
which task is performed first, whereas for other pairs, the order of execution is unimportant.
 The dependencies between tasks can be shown in the form of a digraph. A topological sort
linearizes a digraph. The digraph must not include a cycle; otherwise, a topological sort is
impossible.
 Topological Sort or Topological Ordering of a directed graph is a linear ordering of
its vertices such that for every directed edge uv from vertex u to vertex v, u comes
before v in the ordering.
 A topological ordering is possible if and only if the graph has no directed cycles, that is,
if it is a directed acyclic graph (DAG)
 DAG is a directed graph without any cycle.
 The topological ordering of a DAG is ordering of its nodes in such a way that each node
comes after it’ s predecessor and no successor of any node comes before that node.
Algorithm (Source Removal Algorithm) for topological sorting
 Initially, indegree is computed for all vertices, starting with the vertices which are having
indegree 0. To keep track of vertices with indegree zero we can use a queue. All vertices
of indegree 0 are placed on queue.
 While the queue is not empty, a vertex v is removed, and all edges adjacent to v have their
indegrees decremented. A vertex is put on the queue as soon as its indegree falls to 0.
 The topological ordering is the order in which the vertices DeQueue.

37
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Following are the steps to be followed in this algorithm-


1. Compute the indegree of each vertices in the graph.
2. Find a vertex with indegree zero i.e. vertex with no incoming edges.
 Delete it among with all the edges outgoing from it. If there are more than one such
vertices then break the tie randomly.
(If there is no such vertex, then the graph is cyclic)
 Update the indegree of remaining vertices
 Record the vertex that is deleted (Queue can be used to record the vertex).
3. Repeat step 2 until there is vertex to be processed.
4. The recorded vertices give a topologically sorted list (By performing dequeue operation in
the queue)
Example:
Find a topological ordering for the given graph

38
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Step-01:
Write in-degree of each vertex-

Queue to store deleted vertices


Q=

Step-02:
 Vertex-A has the in-degree zero.
 So, remove vertex-A and its associated edges.
 Add A to Q
 Now, update the in-degree of other vertices.
Q= A

39
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Step-03:
 Vertex-B has in-degree Zero.
 So, remove vertex-B and its associated edges.
 Add B to Q
 Now, update the in-degree of other vertices.
Q= A B

Step-04:
 Vertex - C with indegree zero
 So, remove vertex-B and its associated edges.
 Add C to Q
 Now, update the in-degree of other vertices.

40
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Q= A B C

Step-05:
 Vertex - D with indegree zero
 So, remove vertex-D and its associated edges.
 Add D to Q
 Now, update the in-degree of other vertices

Q= A B C D

Step-06:
 Remove remaining vertex-E and add to Q

0
Q= A B C D E

All vertices of the graph are processed. Now the topological ordering is obtained by performing
dequeue operation in Q.
Hence A, B, C, D, E is a topological ordering
NOTE: DFS based algorithm can also be used to solve topological sorting problem.
Source Vertex: The vertex with no incoming edges.
Sink Vertex / Minimal Vertex: The vertex with no outgoing edges.

41
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Networks
 Network is a digraph with one vertex s, called the source, and one vertex t, called the
sink.
 Network or Flow Network is a directed graph that is used for modeling material flow.
There are two different vertices; one is a source which produces material at some
steady rate, and another one is sink which consumes the content at the same constant
speed. The flow of the material at any mark in the system is the rate at which the
element moves.
 Some real-life problems like the flow of liquids through pipes, the current through
wires and delivery of goods can be modeled using flow networks.
 A Flow Network is a directed graph G = (V, E) such that
 For each edge (u, v) ∈ E, we associate a nonnegative weight capacity
c (u, v) ≥ 0. If (u, v) ∉ E, we assume that c (u, v) = 0.
 There are two distinguishing points, the source s, and the sink t;
 For every vertex v ∈ V, there is a path from s to t containing v.

Example:
A network of pipelines used to deliver water from one source to one destination. However,water
is not simply pumped through one pipe, but through many pipes with many pumping stations in
between. The pipes are of different diameter and the stations are of different power so that the
amount of water that can be pumped may differ from one pipeline to another. For example, the
network in following Figure has eight pipes and six pumping stations. The numbers shown in this
figure are the maximum capacities of each pipeline. For example, the pipe going northeast from
the source s, the pipe sa, has a capacity of 5 units (say, 5,000 gallons per hour).

Figure 4: A pipeline with eight pipes and six pumping stations

42
Collected by Bipin Timalsina
DSA with Java/Unit- 7

The problem is to maximize the capacity of the entire network so that it can transfer the maximum
amount of water. It may not be obvious how to accomplish this goal. Notice that the pipe sa coming
from the source goes to a station that has only one outgoing pipe, ab, of capacity 4. This means
that we cannot put 5 units through pipe sa, because pipe ab cannot transfer it. Also, the amount of
water coming to station b has to be controlled as well because if both incoming pipes, ab and cb,
are used to full capacity, then the outgoing pipe, bt, cannot process it either. It is far from obvious,
especially for large networks, what the amounts of water put through each pipe should be to utilize
the network maximally.
Let G = (V, E) be a flow network. Let s be the source of the network, and let t be the sink. A flow
in G is a real-valued function f: E→R that assigns a number to each edge of the network and
meets following properties:
 Capacity Constraint: The flow through an edge e cannot be greater than its
capacity i.e. 0 ≤ f (e) ≤ cap(e)
 Flow Conservation: The total flow coming to a vertex v is the same as the total
flow coming from it.
i.e. ∑𝑢 𝑓 (𝑒𝑑𝑔𝑒(𝑢𝑣)) = ∑𝑣 𝑓 (𝑒𝑑𝑔𝑒(𝑣𝑤)))
where v is neither the source nor the sink
. In other words, the amount of flow into a v is the same as the amount of
flow out of v for every vertex v ∈ V - {s, t}
Maximum Flow
 It is defined as the maximum amount of flow that the network would allow to flow from
source to sink.
 In the maximum-flow problem, we are given a flow network G with source s and sink t,
and we wish to find a flow of maximum value from s to t. This is called a maximum-flow
(or maxflow) problem
 Multiple algorithms exist in solving the maximum flow problem. Two major algorithms
to solve these kind of problems are
 Ford-Fulkerson algorithm and
 Dinic's Algorithm

43
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Cut
 In graph theory, a cut is a partition of the vertices of a graph into two disjoint subsets. Any cut
determines a cut-set, the set of edges that have one endpoint in each subset of the partition.
 In a flow network, an s–t cut is a cut that requires the source and the sink to be in different
subsets, and its cut-set only consists of edges going from the source's side to the sink's side.
The capacity of an s–t cut is defined as the sum of the capacity of each edge in the cut-set.
 A cut separating s and t is a set of edges between vertices of set 𝑋 and vertices of set
𝑋̅; any vertex of the graph belongs to one of these sets, and source s is in X and sink t
is in 𝑋̅.
 Example:

 In above figure, if 𝑋 = {𝑠, 𝑎}, then 𝑋̅ = {𝑏, 𝑐, 𝑑, 𝑡}, and the cut is the set of
edges {(a,b),(s,c),(s,d)}.
 This means that if all edges belonging to this set are cut, then there is no way to
get from s to t.
 Let us define the capacity of the cut as the sum of capacities of all its edges
leading from a vertex in 𝑋 to a vertex in 𝑋̅; thus,
cap{(a,b),(s,c),(s,d)} = cap(a,b) + cap(s,c) + cap(s,d) = 19.
 The flow through the network cannot be greater than the capacity of any cut.

Max-Flow Min-Cut Theorem


In any network, the maximal flow from s to t is equal to the minimal capacity of any cut.

44
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Ford-Fulkerson algorithm / Method


 Ford-Fulkerson algorithm is a greedy approach for calculating the maximum possible flow in
a network
 By L. R. Ford Jr. and D. R. Fulkerson. (in 1956)
 Some terminologies
Augmenting Path
 It is the path available in a flow network.
 Both the forward and backward edges are considered
 Augmenting path can include :
 Non – full forward edges
 Non – empty backward edges
Residual Graph
 It represents the flow network that has additional possible flow (flow can be
increased or decreased further)
Residual Capacity
 It is the capacity of the edge getting after subtracting the flow from the original
capacity.
 residual capacity = capacity – flow
Bottle Neck Capacity
 It is the minimum residual capacity in an augmenting path.
 This indicates the maximum possible flow from source to sink through the
augmenting path.
bottle neck capacity = minimum residual capacity
Basic Steps in Ford-Fulkerson Method
1. Initialize the flow in all the edges to 0.
2. While there is an augmenting path between the source and the sink, add this path to the
flow and update the residual graph.
3. Return the flow

45
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Algorithm
FordFulkersonAlgo(G,s,t)
flow = 0;
for each edge e in G
flow(e) = 0;
while there is a path, p, from s to t in residual network G_f
for each e in p
residual_capacity(e) = capacity(e) – flow(e) ;
bottleneck_capacity(p) = min(residual_capacity(e) for e in p)
flow = flow + bottleneck_capacity(p);
for each edge e in p
if e is a forward edge
flow(e) = flow(e) + bottleneck_capacity(p);
else
flow(e) = flow(e) - bottleneck_capacity(p);
return flow;

Example: Compute the max flow of following network using Ford Fulkerson Algorithm

4
A B
10 10

S 8 6 T
2

10 10

C D
9

46
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Here S is source and T is sink

Step Augmenting Path Bottle Neck Capacity Flow


0 - - 0
1 S-A-D-T 8 0+8 =8
2 S-C-D-T 2 8+2 = 10
3 S-C-D-A-B-T 4 10+4 = 14
4 S-C-D-B-T 3 14+3 = 17
5 S-A-D-B-T 2 17+2 = 19

Note that if the capacity for any forward edge is full, then that edge cannot be used in other
augmented paths. Similarly if the capacity of backward edge is empty then that edge cannot be
used in other augmented paths.
Step 0:
The flow of all the edges is 0 at the beginning.

0/4
A B
0/10 0/10

S 0/8 0/6 T
0/2

0/10 0/10

C D
0/9

47
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Step 1:
Select any arbitrary path from S to T. In this step, we have selected path S-A-D-T
The minimum residual capacity among the three edges is 8 (A-D). Based on this, update
the flow/capacity for each edge.

0/4
A B
8/10 0/10

S 8/8 0/6 T
0/2

0/10 8/10

C D
0/9

Step 2:
Select another path S-C-D-T. The minimum residual capacity among these edges is 2 (D-T).
Update the flows according to this.

0/4
A B
8/10 0/10

S 8/8 0/6 T
0/2

2/10 10/10

C D
2/9

48
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Step 3:
Another augmenting path is S-C-D-A-B-T. Here, backward edge (reverse edge) DA is considered.
The minimum residual capacity among the edges is 4 (A-B). Update the flows according to this.

4/4
A B
8/10 4/10

S 4/8 0/6 T
0/2

6/10 10/10

C D
6/9

Step 4:
Now, next augmenting path is S-C-D-B-T. The minimum residual capacity among the edges is 3
(C-D). Update the flows according to this.

4/4
A B
8/10 7/10

S 4/8 3/6 T
0/2

9/10 10/10

C D
9/9

49
Collected by Bipin Timalsina
DSA with Java/Unit- 7

Step 5:
Next augmenting path is S-A-D-B-T. The minimum residual capacity among the edges is 2 (S-A).
Update the flows according to this.

4/4
A B
10/10 9/10

S 6/8 5/6 T
0/2

9/10 10/10

C D
9/9

Now SA is also full.


Since there are no possible augmenting paths, We stop here. The max flow of the network is 19

Try Yourself:

50
Collected by Bipin Timalsina

You might also like