You are on page 1of 26

UNIT – 4: Graph

1. Graph
● A Graph G(V,E) is defined as a collection of vertices V and collection
of edges E which connects these vertices. Vertices store the data
elements and edges can represent relationships among these vertices.
● Types of graphs are as follows:
1. Directed Graph
2. Undirected Graph
3. Weighted Graph

1. Undirected Graphs

● A graph can be directed or undirected. However, in an undirected graph,


edges are not associated with the directions with them. An undirected graph
is shown in the above figure since its edges are not attached with any of the
directions. If an edge exists between vertex A and B then the vertices can be
traversed from B to A as well as A to B.

2. Directed Graphs
● A directed graph (or digraph) is a set of nodes connected by edges, where
the edges have a direction associated with them. For example, an arc (x, y)
is considered to be directed from x to y, and the arc (y, x) is the inverted link.
Y is a direct successor of x, and x is a direct predecessor of y.
3. Weighted Graph

● In a weighted graph, each edge is assigned with some data such as length or
weight. The weight of an edge e can be given as w(e) which must be a
positive (+) value indicating the cost of traversing the edge.

2. Graph Terminology

● Path : A path can be defined as the sequence of nodes that are followed in
order to reach some terminal node V from the initial node U.
● Closed Path : A path will be called as closed path if the initial node is same
as terminal node. A path will be closed path if V0=VN.
● Simple Path : If all the nodes of the graph are distinct with an exception
V0=VN, then such path P is called as closed simple path.
● Cycle : A cycle can be defined as the path which has no repeated edges or
vertices except the first and last vertices.
● Connected Graph : A connected graph is the one in which some path exists
between every two vertices (u, v) in V. There are no isolated nodes in
connected graph.
● Complete Graph : A complete graph is the one in which every node is
connected with all other nodes. A complete graph contain n(n-1)/2 edges
where n is the number of nodes in the graph.
● Weighted Graph : In a weighted graph, each edge is assigned with some
data such as length or weight. The weight of an edge e can be given as w(e)
which must be a positive (+) value indicating the cost of traversing the edge.
● Digraph : A digraph is a directed graph in which each edge of the graph is
associated with some direction and the traversing can be done only in the
specified direction.
● Loop : An edge that is associated with the similar end points can be called
as Loop.
● Adjacent Nodes : If two nodes u and v are connected via an edge e, then
the nodes u and v are called as neighbours or adjacent nodes.
● Degree of the Node : A degree of a node is the number of edges that are
connected with that node. A node with degree 0 is called as isolated node.

3. Directed Acyclic Graph (DAG)

• A directed acyclic graph (DAG) refers to a directed graph which has no


directed cycles.
• In graph theory, a graph refers to a set of vertices which are connected by
lines called edges. In a directed graph or a digraph, each edge is associated
with a direction from a start vertex to an end vertex. If we traverse along the
direction of the edges and we find that no closed loops are formed along any
path, we say that there are no directed cycles. The graph formed is a
directed acyclic graph.
• A DAG is always topologically ordered, i.e. for each edge in the graph, the
start vertex of the edge occurs earlier in the sequence than the ending vertex
of the edge.
4. Topological Sorting

• The topological sorting for a directed acyclic graph is the linear ordering of
vertices. For every edge U-V of a directed graph, the vertex u will come
before vertex v in the ordering.
• Topological sorting problem: given digraph G = (V, E) , find a linear ordering
of vertices such that: for any edge (v, w) in E, v precedes w in the ordering.

Topological Sorting Algorithm

topoSort(u, visited, stack)

Input − The start vertex u, An array to keep track of which node is visited or
not. A stack to store nodes.
Output − Sorting the vertices in topological sequence in the stack.
Topological Sorting Example:

The topological orderings of the above graph are found in the following steps-

Step-01:

Write in-degree of each vertex-

Step-02:

• Vertex-A has the least in-degree.

• So, remove vertex-A and its associated edges.

• Now, update the in-degree of other vertices.

Step-03:

• Vertex-B has the least in-degree.


• So, remove vertex-B and its associated edges.

• Now, update the in-degree of other vertices.

Step-04:

● There are two vertices with the least in-degree. So, following 2 cases are
possible-

● In case-01,
Remove vertex-C and its associated edges.

Then, update the in-degree of other vertices.

● In case-02,
Remove vertex-D and its associated edges.

Then, update the in-degree of other vertices.


Step-05:

Now, the above two cases are continued separately in the similar manner.

In case-01,

Remove vertex-D since it has the least in-degree.

Then, remove the remaining vertex-E.

In case-02,

Remove vertex-C since it has the least in-degree.

Then, remove the remaining vertex-E.

Conclusion-

For the given graph, following 2 different topological orderings are possible-

ABCDE

ABDCE

Problem:

Find the number of different topological orderings possible for the given
graph-
5. Graph Traversal Algorithms

● Traversing the graph means examining all the nodes and vertices of the
graph. There are two standard methods by using which, we can traverse
the graphs. Lets discuss each one of them in detail.
1. Breadth First Search
2. Depth First Search

1. Breadth First Search (BFS) Algorithm

● Breadth first search is a graph traversal algorithm that starts traversing the graph
from root node and explores all the neighbouring nodes. Then, it selects the
nearest node and explore all the unexplored nodes. The algorithm follows the
same process for each of the nearest node until it finds the goal.
● The algorithm of breadth first search is given below. The algorithm starts with
examining the node A and all of its neighbours. In the next step, the neighbours
of the nearest node of A are explored and process continues in the further steps.
The algorithm explores all neighbours of all the nodes and ensures that each
node is visited exactly once and no node is visited twice.
● Algorithm
Step 1: SET STATUS = 1 (ready state)
for each node in G
Step 2: Enqueue the starting node A
and set its STATUS = 2
(waiting state)
Step 3: Repeat Steps 4 and 5 until
QUEUE is empty
Step 4: Dequeue a node N. Process it
and set its STATUS = 3
(processed state).
Step 5: Enqueue all the neighbours of
N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT

Example

● Consider the graph G shown in the following image, calculate the minimum path
p from node A to node E. Given that each edge has a length of 1.
● Minimum Path P can be found by applying breadth first search algorithm that will
begin at node A and will end at E. the algorithm uses two queues, namely
QUEUE1 and QUEUE2. QUEUE1 holds all the nodes that are to be processed
while QUEUE2 holds all the nodes that are processed and deleted from
QUEUE1.

Lets start examining the graph from Node A.

1. Add A to QUEUE1 and NULL to QUEUE2.


QUEUE1 = {A}
QUEUE2 = {NULL}
2. Delete the Node A from QUEUE1 and insert all its neighbours. Insert Node A
into QUEUE2
QUEUE1 = {B, D}
QUEUE2 = {A}
3. Delete the node B from QUEUE1 and insert all its neighbours. Insert node B
into QUEUE2.
QUEUE1 = {D, C, F}
QUEUE2 = {A, B}
4. Delete the node D from QUEUE1 and insert all its neighbours. Since F is the
only neighbour of it which has been inserted, we will not insert it again. Insert
node D into QUEUE2.
QUEUE1 = {C, F}
QUEUE2 = { A, B, D}
5. Delete the node C from QUEUE1 and insert all its neighbours. Add node C to
QUEUE2.
QUEUE1 = {F, E, G}
QUEUE2 = {A, B, D, C}
6. Remove F from QUEUE1 and add all its neighbours. Since all of its neighbours
has already been added, we will not add them again. Add node F to QUEUE2.
QUEUE1 = {E, G}
QUEUE2 = {A, B, D, C, F}
7. Remove E from QUEUE1, all of E's neighbours has already been added to
QUEUE1 therefore we will not add them again. All the nodes are visited and the
target node i.e. E is encountered into QUEUE2.
QUEUE1 = {G}
QUEUE2 = {A, B, D, C, F, E}
Now, backtrack from E to A, using the nodes available in QUEUE2.
The minimum path will be A → B → C → E.

2. Depth First Search(DFS) Algorithm

● Depth first search (DFS) algorithm starts with the initial node of the graph G, and
then goes to deeper and deeper until we find the goal node or the node which
has no children. The algorithm, then backtracks from the dead end towards the
most recent node that is yet to be completely unexplored.
● The data structure which is being used in DFS is stack. The process is similar to
BFS algorithm. In DFS, the edges that leads to an unvisited node are called
discovery edges while the edges that leads to an already visited node are called
block edges.

Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting
state)
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 the stack all the neighbours of N that are in the ready state
(whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT

Example :
● Consider the graph G along with its adjacency list, given in the figure below.
Calculate the order to print all the nodes of the graph starting from node H, by
using depth first search (DFS) algorithm.

Push H onto the stack


STACK : H

POP the top element of the stack i.e. H, print it and push all the neighbours of H
onto the stack that are is ready state.
Print H
STACK : A

Pop the top element of the stack i.e. A, print it and push all the neighbours of A
onto the stack that are in ready state.
Print A
Stack : B, D

Pop the top element of the stack i.e. D, print it and push all the neighbours of D
onto the stack that are in ready state.
Print D
Stack : B, F

Pop the top element of the stack i.e. F, print it and push all the neighbours of F
onto the stack that are in ready state.
Print F
Stack : B
Pop the top of the stack i.e. B and push all the neighbours
Print B
Stack : C

Pop the top of the stack i.e. C and push all the neighbours.
Print C
Stack : E, G

Pop the top of the stack i.e. G and push all its neighbours.
Print G
Stack : E

Pop the top of the stack i.e. E and push all its neighbours.
Print E
Stack :

Hence, the stack now becomes empty and all the nodes of the graph have been
traversed.
The printing sequence of the graph will be :
H→A→D→F→B→C→G→E

Differences between BFS and DFS


6. 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..
• By this definition, we can draw a conclusion that every connected and undirected
Graph G has at least one spanning tree. A disconnected graph does not have
any spanning tree, as it cannot be spanned to all its vertices.
● 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.

• Minimum Spanning-Tree Algorithm

• We shall learn about two most important spanning tree algorithms here −

1. Kruskal's Algorithm

2. Prim's Algorithm

Both are greedy algorithms.

1. Kruskal's Algorithm:

Steps for finding MST using Kruskal's Algorithm:


1. Arrange the edge of G in order of increasing weight.
2. Starting only with the vertices of G and proceeding sequentially add each edge
which does not result in a cycle, until (n - 1) edges are used.
3. EXIT.
EXAMPLE:

Step 1 - Remove all loops and Parallel Edges.


In case of parallel edges, keep the one which has the least cost
associated and remove all others.

Step 2 - Arrange all edges in their increasing order of weight


The next step is to create a set of edges and weight, and arrange them in
an ascending order of weightage (cost).

Step 3 - Add the edge which has the least weightage


The least cost is 2 and edges involved are B,D and D,T. We add them. Adding
them does not violate spanning tree properties, so we continue to our next edge
selection.

Next cost is 3, and associated edges are A,C and C,D. We add them again −

Next cost in the table is 4, and we observe that adding it will create a circuit in the
graph. −

We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
We observe that edges with cost 5 and 6 also create circuits. We ignore them
and move on.

Now we are left with only one node to be added. Between the two least cost
edges available 7 and 8, we shall add the edge with cost 7.

By adding edge S,A we have included all the nodes of the graph and we now
have minimum cost spanning tree.

2. Prim's Algorithm

• Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm)
uses the greedy approach. Prim's algorithm shares a similarity with the shortest
path first algorithms.
• Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single
tree and keeps on adding new nodes to the spanning tree from the given graph.
Steps for finding MST using Prim's Algorithm:
1. Create MST set that keeps track of vertices already included in MST.
2. Assign key values to all vertices in the input graph. Initialize all key values as
INFINITE (∞). Assign key values like 0 for the first vertex so that it is picked first.
3. While MST set doesn't include all vertices.
a. Pick vertex u which is not is MST set and has minimum key value. Include
'u'to MST set.
b. Update the key value of all adjacent vertices of u. To update, iterate
through all adjacent vertices. For every adjacent vertex v, if the weight of
edge u.v less than the previous key value of v, update key value as a
weight of u.v.

EXAMPLE:
Step 1 - Remove all loops and parallel edges
Remove all loops and parallel edges from the given graph. In case of parallel
edges, keep the one which has the least cost associated and remove all others.

Step 2 - Choose any arbitrary node as root node


In this case, we choose S node as the root node of Prim's spanning tree. This
node is arbitrarily chosen, so any node can be the root node. One may wonder
why any video can be a root node. So the answer is, in the spanning tree all the
nodes of a graph are included and because it is connected then there must be at
least one edge, which will join it to the rest of the tree.
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges with
weight 7 and 8, respectively. We choose the edge S,A as it is lesser than the
other.
Now, the tree S-7-A is treated as one node and we check for all edges going out
from it. We select the one which has the lowest cost and include it in the tree.

After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and
will check all the edges again. However, we will choose only the least cost edge.
In this case, C-3-D is the new edge, which is less than other edges' cost 8, 6, 4,
etc.

After adding node D to the spanning tree, we now have two edges going out of it
having the same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the
next step will again yield edge 2 as the least cost. Hence, we are showing a
spanning tree with both edges included.
3. Dijkstra's Algorithm

• It is a greedy algorithm that solves the single-source shortest path problem for a
directed graph G = (V, E) with nonnegative edge weights, i.e., w (u, v) ≥ 0 for
each edge (u, v) ∈ E.
• Dijkstra's Algorithm maintains a set S of vertices whose final shortest - path
weights from the source s have already been determined. That's for all vertices v
∈ S; we have d [v] = δ (s, v). The algorithm repeatedly selects the vertex u ∈ V -
S with the minimum shortest - path estimate, insert u into S and relaxes all edges
leaving u.
• Because it always chooses the "lightest" or "closest" vertex in V - S to insert into
set S, it is called as the greedy strategy.
Example:

Step1: Q =[s, t, x, y, z]
We scanned vertices one by one and find out its adjacent. Calculate the distance
of each adjacent to the source vertices.
We make a stack, which contains those vertices which are selected after
computation of shortest distance.
Firstly we take's' in stack M (which is a source)
M = [S] Q = [t, x, y, z]

Step 2: Now find the adjacent of s that are t and y.


Adj [s] → t, y [Here s is u and t and y are v]

Case - (i) s → t
d [v] > d [u] + w [u, v]
d [t] > d [s] + w [s, t]
∞ > 0 + 10 [false condition]
Then d [t] ← 10
π [t] ← 5
Adj [s] ← t, y
Case - (ii) s→ y
d [v] > d [u] + w [u, v]
d [y] > d [s] + w [s, y]
∞>0+5 [false condition]
∞>5
Then d [y] ← 5
π [y] ← 5
By comparing case (i) and case (ii)
Adj [s] → t = 10, y = 5
y is shortest
y is assigned in 5 = [s, y]

Step 3: Now find the adjacent of y that is t, x, z.


Adj [y] → t, x, z [Here y is u and t, x, z are v]
Case - (i) y →t
d [v] > d [u] + w [u, v]
d [t] > d [y] + w [y, t]
10 > 5 + 3
10 > 8
Then d [t] ← 8
π [t] ← y
Case - (ii) y → x
d [v] > d [u] + w [u, v]
d [x] > d [y] + w [y, x]
∞>5+9
∞ > 14
Then d [x] ← 14
π [x] ← 14
Case - (iii) y → z
d [v] > d [u] + w [u, v]
d [z] > d [y] + w [y, z]
∞>5+2
∞>7
Then d [z] ← 7
π [z] ← y
By comparing case (i), case (ii) and case (iii)
Adj [y] → x = 14, t = 8, z =7
z is shortest
z is assigned in 7 = [s, z]

Step - 4 Now we will find adj [z] that are s, x


Adj [z] → [x, s] [Here z is u and s and x are v]
Case - (i) z → x
d [v] > d [u] + w [u, v]
d [x] > d [z] + w [z, x]
14 > 7 + 6
14 > 13
Then d [x] ← 13
π [x] ← z
Case - (ii) z → s
d [v] > d [u] + w [u, v]
d [s] > d [z] + w [z, s]
0>7+7
0 > 14
∴ This condition does not satisfy so it will be discarded.
Now we have x = 13.
Step 5: Now we will find Adj [t]
Adj [t] → [x, y] [Here t is u and x and y are v]
Case - (i) t → x
d [v] > d [u] + w [u, v]
d [x] > d [t] + w [t, x]
13 > 8 + 1
13 > 9
Then d [x] ← 9
π [x] ← t
Case - (ii) t → y
d [v] > d [u] + w [u, v]
d [y] > d [t] + w [t, y]
5 > 10
∴ This condition does not satisfy so it will be discarded.

Thus we get all shortest path vertex as


Weight from s to y is 5
Weight from s to z is 7
Weight from s to t is 8
Weight from s to x is 9
These are the shortest distance from the source's' in the given graph.

You might also like