ADSA UNIT - 3
ADSA UNIT - 3
Definition:
INTRODUCTION TO GRAPHS
Graph Terminology:
We use the following terms in graph data structure.
Vertex
Individual data element of a graph is called as Vertex.
Vertex is also known as node. In above example graph, A, B, C, D
& E are known asvertices.
Edge
An edge is a connecting link between two vertices. Edge is
also
known as Arc. An edge is represented as (startingVertex,
endingVertex).
For example, in above graph the link between vertices A
and B is represented as (A,B). In above example graph, there are
7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).
Undirected Graph
A graph with only undirected edges is said to be undirected
graph.
Directed Graph
A graph with only directed edges is said to be directed graph.
Adjacent
If there is an edge between vertices A and B then both A
and B are said to be adjacent. In other words, vertices A and B
are said to be adjacent if there is an edge between them.
Incident
Edge is said to be incident on a vertex if the vertex is
one of theendpoints of that edge.
Degree
Total number of edges connected to a vertex is said to be
degree ofthat vertex.
Indegree
Total number of incoming edges connected to a vertex is
said to beindegree of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is
said to beoutdegree of that vertex.
Parallel edges or Multiple edges
REPRESENTATION OF GRAPH
By Graph representation, we simply mean the technique which is
to be used in order to store some graph into the computer's
memory. There are two ways to store Graph into the computer's
memory.
• Sequential Representation
• Adjacency Matrix
• Incident Matrix
• Linked Representation
• Adjacency List
• Sequential Representation
• Adjacency Matrix
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 Mij in the adjacency matrix representation of an
undirected graph G will be 1 if there exists an edge between Vi
and Vj.
An undirected graph and its adjacency matrix
representation is shown in the following figure.
in the above figure, we can see the mapping among the vertices
(A, B, C, D, E) is represented by using the adjacency matrix which
is also shown in the figure.
There exists different adjacency matrices for the directed
and undirected graph. In directed graph, an entry Aij will be 1
only whenthere is an edge directed from Vi to Vj.
A directed graph and its adjacency matrix representation
is shownin the following figure.
• Incidence Matrix
In this representation, the graph is represented using a matrix
of size total number of vertices by a total number of edges. That
means graph with 4 vertices and 6 edges is represented using a
matrix of size 4X6. In this matrix, rows represent vertices and
columns represents edges. This matrix is filled with 0 or 1 or -1.
Here, 0 represents that the row edge is not connected to column
vertex, 1 represents that the row edge is connected as the
outgoing edge to column vertex and -1 represents that the row
edge is connected as the incoming edge to column vertex.
• Linked Representation
In the linked representation, an adjacency list is used to store
the Graph into the computer's memory.
• Adjacency List
Consider the undirected graph shown in the following figure
and checkthe 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 checkthe adjacency list representation of the graph.
There are two graph traversal techniques and they are as follows...
• BFS (Breadth First Search)
• DFS (Depth First Search)
• BFS (BREADTH FIRST SEARCH)
BFS traversal of a graph produces a spanning tree as
final result. Spanning Tree is a graph without loops. We use
Queue data structure with maximum size of total number of
vertices in the graph to implement BFS traversal.
ALGORITHM:
//Initially all vertices are unmarked (i.e 0) after visiting they are marked (i.e 1)
BFS(G)
mark each vertex in V with 0 as a mark of being
“unvisited”count ←0
for each vertex v in V do
if v is marked with 0
bfs(v)
bfs(v)
mark v and initialize a
queue with vwhile the
queue is not empty do
for each vertex w in V adjacent to the front vertex do
if w is
mark
ed
with
0
mark
w
add w to the queue
Example:
Example:
Definition:
TOPOLOGICAL SORT
Algorithm:
• Compute all the in degrees of every vertex of given graph G
• Find vertex 'u' which is having the in degree 0 and display it in order.
If no suchvertex is found, then there is possibility that there is cycle
and all the vertices can't be ordered. So, stop the sort process and exit.
• Remove the vertex 'u' and all its corresponding edges (u,v) from given
graph G.
• Update all the in degrees of left out remaining vertices.
• Repeat above stated steps 2 to 4 till all the vertices are processed.
Example:
• Compute all the in degrees of each and every node in the graph .
V1: 0
V2: 1
V3: 2
V4: 2
V5: 2
• Find a vertex with in degree 0 that is V1
• Output V1 and put it in first place in order of
topological sort,remove V1 and update the indegrees
of the left over vertices.
V2: 0
V3: 1
V4: 1
V5: 2
• now the vertex with in degree 0 is V2 ,output V2
,put it into topological sort order next to V1
,remove V2 and update the indegrees of remaining
vertices
V3: 1
V4: 0
V5: 1
• now the vertex with in degree 0 is V4 ,output V4
,put it into topological sort order next to V2
,remove V4 and update the indegrees of remaining
vertices
V3: 0
V5: 0
• now the vertex with in degree 0 is V3 and V5 , we can
follow anyorder here we can either output V3 first or V5
first ,put the selected vertex into topological sort order
next to V4 ,let us takeV3 first remove V3 and at last
remove V5 .
So topological sorting order of the graph is: V1, V2, V4, V3, V5
Complexity
Analysis:
Time Complexity: O(V+E).
The above algorithm is simply DFS with an extra stack. So time
complexity is the same as DFS which is.
Topological Sort Example:
Consider the following directed acyclic graph-
For this
graph, following 4
differenttopological orderings
are possible-
• 123456
• 123465
• 132456
• 132465
Example:
Let us take the graph below.
Initial graph
The strongly connected components of the above graph are:
Kosaraju's Algorithm:
Kosaraju's Algorithm is based on the depth-first searchalgorithm
Three steps are involved.
Let us start from vertex-0, visit all of its child vertices, and
mark the visited vertices as done. If a vertex leads to an
already visited vertex, then push this vertex to the stack.
For example: Starting from vertex-0, go to vertex-1,
vertex-2, and then to vertex-3. Vertex-3 leads to already
visited vertex-0, so push the source vertex (ie. vertex-3)
into the stack.
Stacking
Final Stack
• Reverse the original graph.
DFS on reversed graph
• Perform depth-first search on the reversed graph.
Start from the top vertex of the stack. Traverse through all of
its child vertices. Once the already visited vertex is reached,
one strongly connected component is formed. For example:
Pop vertex-0 from the stack. Starting from vertex-0, traverse
through its child vertices (vertex-0, vertex-1, vertex-2,
vertex-3 in sequence) and mark them as
Definition:
PRIM’S ALGORITHM
Algorithm:
Prim(G)
//Prim’s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = _V, E_
//Output: ET , the set of edges composing a minimum spanning
tree of GVT ← {v0} //the set of tree vertices can be initialized
with any vertex
ET ← ∅
for i ←1 to |V| − 1 do
Example:
In Prim’s Algorithm, we will start with an arbitrary node (it
doesn’t matter which one) and mark it. In each iteration we will
mark a new vertex that is adjacent to the one that we have
already marked. As a greedy algorithm, Prim’s algorithm will
select the cheapest edge and mark the vertex. So we will simply
choose the edge with weight 1. In the next iteration we have
three options, edges with weight 2, 3 and 4. So, we will select
the edge with weight 2 and mark the vertex. Now again we have
three options, edges with weight 3, 4 and 5. But we can’t choose
edge with weight 3 as it is creating a cycle. So we will select the
edge with weight 4 and we end up with the minimum spanning
tree of total cost 7 (= 1 + 2 +4).
Time Complexity:
The time complexity of the Prim’s
Algorithm is O((V+E)logV) because each vertex is inserted in the
priority queue only once and insertion in priority queue take
logarithmic time.
Definition:
KRUSKAL’S ALGORITHM
Algorithm:
Kruskal(G)
//Kruskal’s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = {V, E}
//Output: ET , the set of edges composing a minimum spanning
tree of Gsort E in nondecreasing order of the edge weights
w(ei1) ≤ . . . ≤ w(ei|E|)
ET ← ∅; ecounter ←0 //initialize the set of tree edges and
its sizek←0 //initialize the number of processed edges
while ecounter <
|V| − 1 do
k←k + 1
if ET 𝖴 {eik } is acyclic
ET ← ET 𝖴 {eik}; ecounter ←ecounter + 1
return ET
Example:
Time Complexity:
In Kruskal’s algorithm, most time consuming operation is
sortingbecause the total complexity of the Disjoint-Set operations
will be
O (E log V), which is the overall Time Complexity of the algorithm.
DIJKSTRA’S ALGORITHM
Algorithm:
management.
It is also used by Geographic Information System (GIS),
Definition:
BELLMAN-FORD ALGORITHM
Algorithm:
Input: Graph and a source vertex src.
Output: Shortest distance to all vertices from src. If there is a negative
weight cycle, then shortest distances are not calculated, negative weight
cycle is reported.
bellmanFordAlgorithm(G, s) //G is the graph and s is the
source vertex for each vertex V in G
dist[V] <- infinite // dist is
distance prev[V] <- NULL //
prev is previousdist[s] <- 0
for each
vertex V in G
for each edge
(u,v) in G
temporaryDist <- dist[u] +
edgeweight(u, v)if temporaryDist <
dist[v]
dist[v] <-
temporaryDistprev[v]
<- u
Working of Algorithm:
Like other Dynamic Programming Problems, the algorithm
calculates shortest paths in a bottom-up manner. It first
calculates the shortest distances which have at-most one edge in
the path. Then, it calculates the shortest paths with at-most 2
edges, and so on. After the i-th iteration of the outer loop, the
shortest paths with at most i edges are calculated. There can be
maximum |V| – 1 edges in any simple path that is why the outer
loop runs |v| – 1 times. The idea is, assuming that there is no
negative weight cycle, if we have calculated shortest paths with
at most i edges, then an iteration over all edges guarantees to
give shortest path with at-most (i+1) edges.
Example:
Consider the weighted graph below.
Choose path value 0 for the source vertex and infinity for
all other vertices.
Definition:
DYNAMIC PROGRAMMING
Example:
For example, if we write simple recursive solution for
FibonacciNumbers, we get exponential time complexity and if
we optimize it by
ALL-PAIRS SHORTEST
PATHS –WARSHALL’s ALGORITHM
Transitive closure definition: The transitive closure of a
directed graph with n vertices can be defined as the n × n
boolean matrix T = {tij}, in which the element in the ith row and
the jth column is 1 if there exists a nontrivial path (i.e., directed
path of a positive length) from the ith vertex to the jth vertex;
otherwise, tij is 0.
return R(n)
Working of algorithm:
Example:
MATRIX MULTIPLICATION USING
DYNAMIC
PROGRAMMING
The first step is to create a matrix where the number of rows and
columns equals the number of vertices and then to populate it
with initial data. For each vertex that can be reached by one
hop, you will populate it with the edge weight. If a vertex cannot
be reached with one hop, you will populate it with infinity and if
a vertex is trying to reach itself, you will populate it with zero.
Let’s walk through the algorithm to populate the initial matrix.
We’ll start by populating all the zeros. The zeros will be
down thediagonal. Why? Looking at the first entry at A1(1,1),
you’re trying to go
From 3 to 2, A0(3,2) = 4
From 3 to 4,
A0(3,4) = ∞
From 4 to 1,
A0(4,1) = ∞
From 4 to 2,
A0(4,2) = -3
From 4 to 3, A0(4,3) = 1
We start with trying to obtain the value for A0(1,1). If we follow
the for loop, k will be equal to 1, then 2, then 3 and finally 4
while the values of iand j do not change.
The minimum value is 0. So, we take the value of zero and plug
it into A1(1,1)