Unit 4
Unit 4
net
GRAPH-BREADTH FIRST TRACERSAL-EPTH FIRST TRAVERSAL -TOPOLOGICAL
SORT-BI-CONNECTIVITY-CUT VERTEX-EUCLER CIRCUITS-APPLICATIONS OF HEAP.
Application of graphs:
Coloring of MAPS
Representing network
o Paths in a city
o Telephone network o
Electrical circuits etc.
It is also using in social network
including o LinkedIn
o Facebook
Types of Graphs:
Directed graph
Undirected Graph
Directed Graph:
In representing of graph there is a directions are
shown on the edges then that graph is called
Directed graph.
That is,
A graph G=(V, E) is a directed graph ,Edge is a
Undirected Graph:
In graph vertices are not ordered is called undirected
graph. Means in which (graph) there is no direction
(arrow head) on any line (edge).
A graph G=(V, E) is a directed graph ,Edge is a pair
Weight graph:
Edge may be weight to show that there is
a cost to go from one vertex to another.
Example: In graph of roads (edges) that
connect one city to another (vertices), the
weight on the edge might represent the
distance between the two cities (vertices).
www.padeepz.net
www.padeepz.net
Difference between Trees and Graphs
Trees Graphs
Path Tree is special form of graph i.e. minimally In graph there can be more than
connected graph and having only one path one path i.e. graph can have uni-
between any two vertices. directional or bi-directional paths
(edges) between nodes
Loops Tree is a special case of graph having Graph can have loops, circuits as
no loops, no circuits and no self-loops. well as can have self-loops.
Root Node In tree there is exactly one root node and In graph there is no such concept
every child have only one parent. of root node.
Parent In trees, there is parent child relationship so In Graph there is no such parent
Child flow can be there with direction top to child relationship.
relationship bottom or vice versa.
Complexity Trees are less complex then graphs as having Graphs are more complex in
no cycles, no self-loops and still connected. compare to trees as it can have
cycles, loops etc
Types of Tree traversal is a kind of special case of Graph is traversed by
Traversal traversal of graph. Tree is traversed in Pre- DFS: Depth First Search
Order, In-Order and Post-Order(all three BFS : Breadth First Search
in DFS or in BFS algorithm) algorithm
Connection In trees, there are many rules / restrictions In graphs no such rules/
Rules for making connections between nodes restrictions are there for
through edges. connecting the nodes through
edges.
DAG Trees come in the category of DAG : Graph can be Cyclic or Acyclic.
Directed Acyclic Graphs is a kind of
directed graph that have no cycles.
Different Different types of trees are : Binary Tree , There are mainly two types of
Types Binary Search Tree, AVL tree, Heaps. Graphs :Directed and Undirected
graphs.
Applications Tree applications: sorting and searching like Graph applications : Coloring of
Tree Traversal & Binary Search. maps, in OR (PERT & CPM),
algorithms, Graph coloring, job
scheduling, etc.
No. of edges Tree always has n-1 edges. In Graph, no. of edges depends on
the graph.
Model Tree is a hierarchical model. Graph is a network model.
Figure
www.padeepz.net
Other types of graphs:
www.padeepz.net
Complete Graph:
A complete graph is a simple undirected graph in which every pair of distinct vertices is
connected by a unique edge.
OR
If an undirected graph of n vertices consists of n(n-1)/2 number of edges then the graph is
called complete graph.
Example:
vertices Edges Complete graph vertices Edges Complete graph
n=2 1 n=6 15
n=3 3 n=7 21
n=4 6 n=5 10
www.padeepz.net
Sub graph:
www.padeepz.net
A sub-graph G' of graph G is a graph, such that the set of vertices and set of edges
of G' are proper subset of the set of vertices and set of edges of graph G
respectively.
Connected Graph:
A graph which is connected in the sense of a topological space (study of shapes), i.e., there is
a path from any point to any other point in the graph. A graph that is not connected is said to
be disconnected.
www.padeepz.net
Path:
www.padeepz.net
A path in a graph is a finite or infinite sequence of edges which connect a sequence of
vertices. Means a path form one vertices to another vertices in a graph is represented by
collection of all vertices (including source and destination) between those two vertices.
Simple Cycle: a cycle that does not pass through other vertices more than once
Degree:
The degree of a graph vertex v of a graph G is the number of graph edges which touch v. The
vertex degree is also called the local degree or valency. Or
The degree (or valence) of a vertex is the number of edge ends at that vertex.
For example, in this graph all of the vertices have degree three.
In a digraph (directed graph) the degree is usually divided into the in-degree and the out-
degree
In-degree: The in-degree of a vertex v is the number of edges with v as their terminal
vertex.
Out-degree: The out-degree of a vertex v is the number of edges with v as their initial
vertex.
www.padeepz.net
www.padeepz.net
TOPOLOGICAL SORT
A topological sort is a linear ordering of vertices in a Directed Acyclic Graph
such that if there is a path from Vi to Vp, then Vj appears after Vi in the linear
ordering.Topological sort is not possible if the graph has a cycle.
INTRODUCTION
PROCEDURE
Step – 3 : Dequeue the vertex V and decrement the indegrees of all its adjacent
vertices.
Step – 4 : Enqueue the vertex on the queue if its indegree falls to zero.
Vertices Indegree
1 0
2 0
www.padeepz.net
www.padeepz.net
GRAPH TRAVERSAL
Graph traversal is the Visiting all the nodes of a graph.
The Breadth first search was one of the systematic approaches for
exploring and searching the vertices/nodes in a given graph. The
approach is called "breadth-first" because from each vertex ‘v’ that we
visit, we search as broadly as possible by next visiting all the vertices
adjacent to v.
It can also be used to find out whether a node is reachable from a given
node or not.
It is applicable to both directed and undirected graphs.
Queue is used in the implementation of the breadth first search.
www.padeepz.net
www.padeepz.net
ALGORITHM
Procedure bfs()
{
//BFS uses Queue data structure
Queue q=new LinkedList();
q.add(this.rootNode);
printNode(this.rootNode);
rootNode.visited=true;
while(!q.isEmpty())
{
Node n=(Node)q.remove(); Node child=null;
while((child=getUnvisitedChildNode(n))!=null)
{
child.visited=true;
printNode(child);
q.add(child);
}
}
//Clear visited property of nodes
clearNodes();
}
Procedure
Step -1 Select the start vertex/source vertex. Visit the vertex and mark it
as one (1) (1 represents visited vertex).
Step -2 Enqueue the vertex.
Step -3 Dequeue the vertex.
Step -4 Find the Adjacent vertices.
Step -5 Visit the unvisited adjacent vertices and mark the distance as 1.
Step -6 Enqueue the adjacent vertices.
Step -7 Repeat from Step – 3 to Step – 5 until the queue becomes empty.
www.padeepz.net
www.padeepz.net
Ex : A B Vertices Visited
Vertices
A 1
C D B 0 1
C 0 1
D 0 1
Enqueue A B C D
Dequeue A B C D
A B
Vertices Visited
Vertices
C 1
C D A 0 1
B 0 1
D 0 1
E E 0 1
Enqueue C A B D
E
Dequeue C A B D
E
Pseudo Code :
void BFS(Vertex S)
Vertex v,w;
Queue Q;
visited[s] = 1; enqueue(S,Q);
while(!IsEmpty(a))
www.padeepz.net
www.padeepz.net
v = Dequeue(Q);
print(v);
if(visited[w]==0)
visited[w] = 1;
Enqueue(w,Q);
APPLICATIONS
Breadth-first search can be used to solve many problems in graph theory, for
example.
Uses
www.padeepz.net
www.padeepz.net
Testing whether graph is connected.
Computing a spanning forest of graph.
Computing, for every vertex in graph, a path with the minimum number
of edges between start vertex and current vertex or reporting that no
such path exists.
Computing a cycle in graph or reporting that no such cycle exists.
www.padeepz.net
www.padeepz.net
This entire process is repeated until all vertices are discovered.
Stack is used in the implementation of the depth first search.
In DFS, the basic data structures for storing the adjacent nodes is stack.
Procedure:
www.padeepz.net
www.padeepz.net
Pseudo Code:
void DFS(Vertex S)
int top = 0;
visited [S] = 1
C[top] = S;
while(!IsEmpty(C))
v = pop(C);
print(V);
if(visited[w] == 0)
visited[w] = 1;
push(w,C);
(OR)
www.padeepz.net
www.padeepz.net
void DFS(vertex V)
visited[v] = 1;
push(v);
pop(v);
if(!visited[w])
Dfs(w);
ALGORITHM
Procedure dfs()
{
//DFS uses Stack data structure
Stack s=new Stack();
s.push(this.rootNode);
rootNode.visited=true;
printNode(rootNode);
while(!s.isEmpty())
{
Node n=(Node)s.peek();
Node child=getUnvisitedChildNode(n);
if(child!=null)
{
child.visited=true;
printNode(child);
s.push(child);
}
else
{
s.pop();
www.padeepz.net
www.padeepz.net
}
}
//Clear visited property of nodes
clearNodes();
}
Applications of DFS :
I. Undirected Graph :
An undirected graph is connected if and only if a depth first search
starting from any node visits every node.
A 1. Tree Edge
B D E
A A
C B B
C E C
D D
B A C
G B
C D A
www.padeepz.net
www.padeepz.net
F D
G E F
E
A
B
C D
G E
II. Bi-Connectivity :
B A A (1,1)
B (2,1)
C D (3,1) C G (7,7)
www.padeepz.net
www.padeepz.net
F D (4,1)
G E E (5,4)
F (6,4)
BICONNECTIVITY :
A connected undirected graph is biconnective if there are no vertices
whose removal disconnects the rest of the graph.
DEFINITION
containing u and v.
Biconnected Components
www.padeepz.net
www.padeepz.net
Interaction of biconnected components
Articulation Point :
The root is an articulation if and only if (iff) it has more than two children.
Any vertex V other than the root is an Articulation point iff V has some child W
such that Low(W)≥Num (V)
A (1,1)
B (2,1)
C (3,1)
D (4,1) G (7,7)
www.padeepz.net
www.padeepz.net
E (5,4)
F (6,4)
= min(6, 4, -1)
=4
Low(E) = min(5, 6, 4)
=4
Low(D) = min(4, 1, 4)
=1
Low(G) = min(7, -, -)
=7
=1
Low(B) = min(2, 3, 1)
=1
Low(A) = min(1, 2, 1)
=1
Example 2
B E H I
www.padeepz.net
www.padeepz.net
A C F I K
D
G
A F
D G E
I
Visited Parent[]
1 2 3 4
1 1 1 1 1 1 1 1
A B C D 0 1 2 3
0 1 2 3
1 1 1 1
Num[] A B C D
A A (1,1)
B C B (2,1)
D C (3,1)
www.padeepz.net
www.padeepz.net
D (4,4)
ALGORITHM
void AssignNum(Vertex V)
{
Vertex W;
int counter = 0;
Num[V] = ++counter;
visited[V] = True;
for each W adjacent to V
if(!visited[W])
{
parent[W] = V;
AssignNum(W);
}
}
void AssignLow(Vertex V)
{
Vertex W;
Low[V] = Num[V]; /* Rule 1 */
for each W adjacent to V
{
if(Num[W] > Num[V]) /* forward edge or free edge */
{ AssignLow(W)
if(low[W] >= Num[V])
printf(“%v Articulation point is”, V);
Low[V] = min(Low[V], Low[W]); /* Rule 3 */
}
else
{
if(parent[V]! = W) /* Back edge */
Low[V] = min(Low[V], Num[W]); /* Rule 2 */
www.padeepz.net
www.padeepz.net
}
}
}
APPLICATION
BICONNECTIVITY ADVANTAGES
DISADVANTAGES
EULER CIRCUIT
EULERIAN PATH
EULERIAN CIRCUIT
www.padeepz.net
www.padeepz.net
Unicursal. While such graphs are Eulerian graphs, not every Eulerian graph
possesses an Eulerian cycle.
EULER'S THEOREM
Euler's theorem 1
If a graph has any vertex of odd degree then it cannot have an Euler
circuit.
If a graph is connected and every vertex is of even degree, then it at least
has one Euler circuit.
Euler's theorem 2
If a graph has more than two vertices of odd degree then it cannot have
an Euler path.
If a graph is connected and has just two vertices of odd degree, then it at
least has one Euler path. Any such path must start at one of the odd-
vertices and end at the other odd vertex.
ALGORITHM
1. Check to make sure that the graph is connected and all vertices are of
even degree
2. Start at any vertex
3. Travel through an edge:
o If it is not a bridge for the untraveled part, or
o there is no other alternative
4. Label the edges in the order in which you travel them.
5. When you cannot travel any more, stop.
www.padeepz.net
www.padeepz.net
Fleury's Algorithm
the original graph minus the darkened (already used) edges = reduced
graph
important rule: never cross a bridge of the reduced graph unless there is
no other choice
Note:
APPLICATION
www.padeepz.net
www.padeepz.net
IMPORTANT SUMS
1. Topological Sort
V2 V3 VerticesIndegree
V1 0
V2 1 0
V3 1 0 0
V1 V4 V5 V6 V10 V4 1 0
V5 1 0
V6 2 1 0
V7 V8 V9 V7 2 1
0
V8 1 0
V9 1 0
V10 1 0
Enqueue V1 V2 V4 V3 V5 V10 V6
V9 V8 V7
Sorted Vertices : Dequeue V1 V2 V4 V3 V5 V10 V6
V9 V8 V7
V1 V2 V4 V3 V5 V10 V6 V9 V8 V7
www.padeepz.net
www.padeepz.net
b c
2
a d
6
6 4
f e
void Topsort(Graph G)
Queue Q;
Make Empty(Q);
if (Indegree[V]==0)
enqueue(V,Q);
while(!Isempty(Q))
V = Dequeue(Q);
Topnum[V]=++Counter;
www.padeepz.net
www.padeepz.net
if(--Indegree[w]==0)
Enqueue(w,0);
if(counter!=Num Vertex)
Dispose Queue(Q);
void unweighted(Table T)
Queue Q;
Vertex v,w;
Make Empty(Q);
while(!IsEmpty(Q))
V = Dequeue(o);
T[V].Known = True;
if(!T[w].Known)
www.padeepz.net