You are on page 1of 38

ADA

GRAPHS AND ITS TRAVERSAL

Dr. Puneet Singh Lamba


Assistant Prof.
Dr. Puneet Singh Lamba
SGTB KHALSA COLLEGE
Graphs
A graph G is simply a way of encoding pairwise relationships
among a set of objects: it consists of a collection V of nodes and a
collection E of edges, each of which "joins" two of the nodes. We
thus represent an edge e ∑ E as a two-element subset of V: e = {u,
v} for some u, v ∑ V, where we call u and v the ends of e.

Used in:
• Transportation networks
• Communication networks
• Information networks
• Social networks
• Dependency Networks

Dr. Puneet Singh Lamba


Representation

Dr. Puneet Singh Lamba


Directed Graph

Dr. Puneet Singh Lamba


Path
• With this notion in mind, we define a path in an undirected graph G = (V, E) to be a
sequence P of nodes v1, v2 ..... vk-1, vk with the property that each consecutive pair vi,
vi+1 is joined by an edge in G.
• P is often called a path from v1 to vk, or a v1-vk path. For example, the nodes 4, 2, 1, 7,
8 form a path in Figure (next slide).
• A path is called simple if all its vertices are distinct from one another.
• A cycle is a path v1, v2 ..... vk-1, vk in which k > 2, the first k – 1 nodes are all distinct,
and vl = vk
• In other words, the sequence of nodes "cycles back" to where it began.
• All of these definitions carry over naturally to directed graphs, with the following
change: in a directed path or cycle, each pair of consecutive nodes has the property
that (vi, vi+l) is an edge.
• We say that an undirected graph is connected if, for every pair of nodes u and v, there
is a path from u to v. We say that a directed graph is strongly connected if, for every
two nodes u and u, there is a path from u to v and a path from v to u.
Dr. Puneet Singh Lamba
Dr. Puneet Singh Lamba
Dr. Puneet Singh Lamba
BFS (Breadth First Search)
•In the example of Figure, starting
with node 1 as s, the first layer of
the search would consist of nodes
2 and 3, the second layer would
consist of nodes 4, 5, 7, and 8, and
the third layer would consist just
of node 6.
•At this point the search would
stop, since there are no further
nodes that could be added (and in
particular, note that nodes 9
through 13 are never reached by
the search).

Dr. Puneet Singh Lamba


BFS
ng
cti
ru
nst
Co
Dr. Puneet Singh Lamba
Dr. Puneet Singh Lamba
Theorems

Dr. Puneet Singh Lamba


Dr. Puneet Singh Lamba
Dr. Puneet Singh Lamba
Dr. Puneet Singh Lamba
Breadth First Search

• Explores all nodes at given depth before proceeding to next level


• Implemented through queue

Algorithm
1. Enter starting node on Q
2. If Q is empty, return fail and stop
3. If 1st element on Q is Goal, Success and exit
4. Remove and expand 1st element from Q, place children at end of Q
5. Goto step 2

Dr. Puneet Singh Lamba


Dr. Puneet Singh Lamba
BFS

Q={A}
B C Q={B,C}
Q={C,D,E}
D E F Q={D,E,F}
Q={E,F,G}
Q={F,G}
G H
Q={G,H} GOAL
Q={H}
Q={}
A->B->C->D->E->F->G->H
Dr. Puneet Singh Lamba
BFS

Advantages:
• Find a solution if it exists
• Minimal Sol in least no. steps

Disadvantages:
• Requires more memory
• Needs time if sol is far from root

T.C= O(b^d)
b: branching factor
d: depth

Dr. Puneet Singh Lamba


DFS

Dr. Puneet Singh Lamba


Dr. Puneet Singh Lamba
Depth First Search

• Recursive Algo
• Starts from the root and each path to its greatest depth node before
moving to next node
• Stack LIFO

Algo:
1. Root on the stack
2. Do until stack is not empty
1. Pop node
2. If node is Goal, stop
3. Push all children of node on Stack

Dr. Puneet Singh Lamba


DFS

A
A

B C D G
B E E E
C C C C
D E F

G H H
C F

A->B->D->G->E->C->F->H

Dr. Puneet Singh Lamba


DFS

Advantages:
• Less m/m
• Less time to reach goal if traversal in right path

Disadvantages:
• No guarantee of finding sol. (infinite L/R)
• Infinite

T.C=O(b^d)
S.C=O(bd)

Dr. Puneet Singh Lamba


DFS Tree for Water Jug problem
(0, 0) Start State

(4, 0)

(4, 3) (0, 0) (1, 3)

(0, 3) (4, 0)

 (4, 3)  (0, 0) (3, 0)

(3, 3) (0, 0) (0, 3)

 (3, 0)  (0, 3) (4, 2)

(0, 2) (3, 3) (4, 0)

Dr. Puneet Singh Lamba


(4, 2) (0, 0) (2, 0)
BFS Animation

Dr. Puneet Singh Lamba


BFS

Dr. Puneet Singh Lamba


DFS

Dr. Puneet Singh Lamba


Apply BFS & DFS

A H

B C I J

D E G K

Dr. Puneet Singh Lamba


Bipartite Graph

•A Bipartite Graph is a graph


whose vertices can be divided
into two independent sets, U and
V such that every edge (u, v)
either connects a vertex from U
to V or a vertex from V to U.
•In other words, for every edge
(u, v), either u belongs to U and v
to V, or u belongs to V and v to U.
We can also say that there is no
edge that connects vertices of
same set.

Dr. Puneet Singh Lamba


•A bipartite graph is possible if the
graph coloring is possible using
two colors such that vertices in a
set are colored with the same
color. Note that it is possible to
color a cycle graph with even cycle
using two colors. For example, see
the following graph.

Dr. Puneet Singh Lamba


•It is not possible to color a
cycle graph with odd cycle
using two colors.

Dr. Puneet Singh Lamba


ALGORITHM
Algorithm to check if a graph is Bipartite:
One approach is to check whether the graph is 2-colorable or not using
backtracking algorithm m coloring problem. Following is a simple algorithm to find out whether
a given graph is Bipartite or not using Breadth First Search (BFS).

1. Assign RED color to the source vertex (putting into set U).
2. Color all the neighbors with BLUE color (putting into set V).
3. Color all neighbor’s neighbor with RED color (putting into set U).
4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring
problem where m = 2.
5. While assigning colors, if we find a neighbor which is colored with same color as current
vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite)

Time Complexity : O(V*V) as adjacency matrix is used for graph but


can be made O(V+E) by using adjacency list
Dr. Puneet Singh Lamba
Auxiliary Space: O(V) due to queue and color vector
Topological Sort
(Application of DFS)

Dr. Puneet Singh Lamba


Dr. Puneet Singh Lamba
Dr. Puneet Singh Lamba
Dr. Puneet Singh Lamba
Strongly Connected
Components

Dr. Puneet Singh Lamba


Dr. Puneet Singh Lamba

You might also like