Unit II Notes
Unit II Notes
Basic concepts
Definition
Directed
Undirected
Directed graph
graph. Example
Directed Graph
Undirected graph
graph. Example
Undirected Graph
Representation of Graphs
Graph data structure is represented using the following
representations.
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
Adjacency list
Adjacency List
Graph traversals
Applications of graphs
1. Social network graphs : To tweet or not to tweet. Graphs
that represent who knows whom, who communicates with
whom, who influences whom, or other relationships in
social structures. An example is the twitter graph of who
follows whom.
2. Graphs in epidemiology: Vertices represent individuals
and directed edges to view the transfer of an infectious
disease from one individual to another. Analyzing such
graphs has become an important component in
understanding and controlling the spread of diseases.
3. Protein-protein interactions graphs: Vertices represent
proteins and edges represent interactions between them
that carry out some biological function in the cell. These
graphs can be used to, for example, study molecular
pathway—chains of molecular interactions in a cellular
process.
4. Network packet traffic graphs: Vertices are IP (Internet
protocol) addresses and edges are the packets that flow
between them. Such graphs are used for analyzing
network security, studying the spread of worms, and
tracking criminal or non- criminal activity.
5. Neural networks: Vertices represent neurons and edges
are the synapses between them. Neural networks are
used to understand how our brain works and how
connections change when we learn. The human brain has
about 1011 neurons and close to 1015 synapses.
As in the example given above, DFS algorithm traverses from S to A to D to G to E to B first, then to F
and lastly to C. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all
the vertices from the stack, which do not have adjacent vertices.)
5
We choose B, mark it as visited and
put onto the stack. Here B does not
have any unvisited adjacent node. So,
we pop B from the stack.
6
We check the stack top for return to
the previous node and check if it has
any unvisited nodes. Here, we
find D to be on the top of the stack.
7
Only unvisited adjacent node is
from D is C now. So we visit C, mark it
as visited and put it onto the stack.
Pseudocode of DFS
Downloaded from
EnggTree.com
EnggTree.co
DFS(G, u) m
for each v ∈
u.visited = true
G.Adj[u] if v.visited
== false
DFS(G,v)
For each u ∈ G
init() {
For each u ∈ G
u.visited = false
DFS(G, u)
}
As in the example given above, BFS algorithm traverses from A to B to E to F first then to C and G
lastly to D. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Downloaded from
EnggTree.com
EnggTree.co
Step Traversal m Description
4
Next, the unvisited adjacent node
from S is B. We mark it as visited and
enqueue it.
5
Next, the unvisited adjacent node
from S is C. We mark it as visited and
enqueue it.
6
Now, S is left with no unvisited
adjacent nodes. So, we dequeue and
find A.
7
From A we have D as
unvisited adjacent node. We
mark it as visited and
enqueue it.
Downloaded from
EnggTree.com
EnggTree.co
m
Downloaded from
EnggTree.com
EnggTree.co
m
BFS pseudocode
create a queue Q
while Q is non-empty
Downloaded from
EnggTree.com
EnggTree.co
m
stack STACK
void DFS(int source) {
visited[s]=true
for all neighbours X of source that are not visited:
DFS(X)
STACK.push(source)
}
CLEAR ADJACENCY_LIST
for all edges e:
first = one end point of e
second = other end point of e
ADJACENCY_LIST[second].push(first)
Bi Connectivity Graph
An undirected graph is said to be a biconnected graph, if there are two vertex-disjoint paths
between any two vertices are present. In other words, we can say that there is a cycle between any
two vertices.
We can say that a graph G is a bi-connected graph if it is connected, and there are no articulation
points or cut vertex are present in the graph.
Downloaded from
EnggTree.com
EnggTree.co
To solve this problem, we will use the DFSmtraversal. Using DFS, we will try to find if there is any
articulation point is present or not. We also check whether all vertices are visited by the DFS or not,
if not we can say that the graph is not connected.
Downloaded from
EnggTree.com
EnggTree.co
2. Kruskal’s Algorithm m
Downloaded from
EnggTree.com
EnggTree.co
m
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
Choose a vertex
Choose the nearest edge not yet in the solution, if there are multiple choices, choose one at random
Downloaded from
EnggTree.com
EnggTree.co
m
T = ∅;
one, we move vertices from set V-U to set U by connecting the least weight edge.
U = { 1 };
T = T ∪ {(u, v)}
U = U ∪ {v}
Kruskal Algorithm
Kruskal'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 Kruskal's algorithm works
It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes
of finding a global optimum.
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:
1. Sort all the edges from low weight to high
2. 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.
3. Keep adding edges until we reach all vertices.
Choose the edge with the least weight, if there are more than 1, choose anyone
Downloaded from
EnggTree.com
EnggTree.co
m
Choose the next shortest edge that doesn't create a cycle and add it
Choose the next shortest edge that doesn't create a cycle and add it
A=∅
KRUSKAL(G):
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
MAKE-SET(v)
A = A ∪ {(u, v)}
if FIND-SET(u) ≠ FIND-SET(v):
UNION(u, v)
return A
The shortest path problem is about finding a path between vertices in a graph such that the total
sum of the edges weights is minimum.
Bellman Algorithm
Bellman Ford algorithm helps us find the shortest path from a vertex to all other vertices of a
weighted graph.
It is similar to Dijkstra's algorithm but it can work with graphs in which edges can have negative
weights.
Downloaded from
EnggTree.com
EnggTree.co
How Bellman Ford's algorithm works m
Bellman Ford algorithm works by overestimating the length of the path from the starting vertex to
all other vertices. Then it iteratively relaxes those estimates by finding new paths that are shorter
than the previously overestimated paths.
By doing this repeatedly for all vertices, we can guarantee that the result is optimized.
Time Complexity
Dijkstra Algorithm
Dijkstra's algorithm allows us to find the shortest path between any two vertices of a graph.
It differs from the minimum spanning tree because the shortest distance between two vertices
might not include all the vertices of the graph.
Djikstra used this property in the opposite direction i.e we overestimate the distance of each vertex
from the starting vertex. Then we visit each node and its neighbors to find the shortest subpath to
those neighbors.
The algorithm uses a greedy approach in the sense that we find the next best solution hoping that
the end result is the best solution for the whole problem.
Choose a starting vertex and assign infinity path values to all other devices
After each iteration, we pick the unvisited vertex with the least path length. So we choose 5 before 7
Notice how the rightmost vertex has its path length updated twice
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).
A weighted graph is a graph in which each edge has a numerical value associated with it.
Floyd-Warhshall algorithm is also called as Floyd's algorithm, Roy-Floyd algorithm, Roy-Warshall
algorithm, or WFI algorithm.
This algorithm follows the dynamic programming approach to find the shortest paths.
Initial graph
Follow the steps below to find the shortest path between all the pairs of vertices.
1. 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.
Fill each cell with the distance between ith and jth vertex
2. 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. In this
step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).
That is, if the direct distance from the source to the destination is greater than the path
h the vertex k, then the cell is filled with A[i][k] + A[k][j].
In this step, k is vertex 1. We calculate the distance from source vertex to destination vertex
through this vertex
k. Calcula
te the distance from the source vertex to destination vertex through this vertex k
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the
distance from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to 4) is 7.
Since 4 < 7, A0[2, 4] is filled with 4.
3. 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
1. Calcula
te the distance from the source vertex to destination vertex through this vertex 2
Calculat
e the distance from the source vertex to destination vertex through this
vertex C
alculate the distance from the source vertex to destination vertex through this vertex 4
5. A4 gives the shortest path between each pair of vertices.
Floyd-Warshall Algorithm
n = no of vertices
A = matrix of dimension n*n
for k = 1 to n
for i = 1 to n
for j = 1 to n
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
return A
Time Complexity
There are three loops. Each loop has constant complexities. So, the time complexity of the Floyd-
Warshall algorithm is O(n3).
Network Flow
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 modelled using flow networks.
1. For each edge (u, v) ∈ E, we associate a nonnegative weight capacity c (u, v) ≥ 0.If (u, v) ∉ E,
Definition: A Flow Network is a directed graph G = (V, E) such that
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: V x V→R such that the following properties hold:
The quantity f (u, v), which can be positive or negative, is known as the net flow from vertex u to
vertex v. In the maximum-flow problem, we are given a flow network G with source s and sink t, and
a flow of maximum value from s to t.
Ford-Fulkerson Algorithm
Initially, the flow of value is 0. Find some augmenting Path p and increase flow f on each edge of p by
residual Capacity cf (p). When no augmenting path exists, flow f is a maximum flow.
FORD-FULKERSON METHOD (G, s, t)
1. Initialize flow f to 0
2. while there exists an augmenting path p
3. do argument flow f along p
4. Return f
2. do f [u, v] ← 0
3. f [u, v] ← 0
4. while there exists a path p from s to t in the residual network Gf.
5. do cf (p)←min?{ Cf (u,v):(u,v)is on p}
6. for each edge (u, v) in p
7. do f [u, v] ← f [u, v] + cf (p)
8. f [u, v] ←-f[u,v]
Example: Each Directed Edge is labeled with capacity. Use the Ford-Fulkerson algorithm to find the
maximum flow.
Solution: The left side of each part shows the residual network Gf with a shaded augmenting path
p,and the right side of each part shows the net flow f.
Maximum Bipartite Matching
The bipartite matching is a set of edges in a graph is chosen in such a way, that no
two edges in that set will share an endpoint. The maximum matching is matching
the maximum number of edges.
When the maximum match is found, we cannot add another edge. If one edge is
added to the maximum matched graph, it is no longer a matching. For a bipartite
graph, there can be more than one maximum matching is possible.
Algorithm