0% found this document useful (0 votes)
2 views18 pages

5

This document covers various aspects of graph structures, including definitions, representations, and traversal methods. It explains concepts such as directed and undirected graphs, weighted graphs, and traversal algorithms like breadth-first search and depth-first search. Additionally, it discusses topological sorting and the properties of different types of graphs, including complete and acyclic graphs.

Uploaded by

gokilam.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

5

This document covers various aspects of graph structures, including definitions, representations, and traversal methods. It explains concepts such as directed and undirected graphs, weighted graphs, and traversal algorithms like breadth-first search and depth-first search. Additionally, it discusses topological sorting and the properties of different types of graphs, including complete and acyclic graphs.

Uploaded by

gokilam.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT-V GRAPH STRUCTURES 9

Graph Definition – Representation of Graphs – Types of Graph - Breadth-first traversal –


Depth-first traversal –– Bi-connectivity –Topological Sort – Dijkstra's algorithm –
Minimum Spanning Tree – Prim's algorithm – Kruskal's algorithm.

DEFINITION
A graph G = (V, E) consists of a set of vertices, V and set of edges E.
Vertics are referred to as nodes and the arc between the nodes are referred to as Edges.
Each edge is a pair (v, w) where v, w  V. (i.e.) v = V1, w = V2...

V1 V2

V4 V3

Here V1, V2, V3, V4 are the vertices and (V1, V2), (V2, V3), (V3, V4), (V4, V1), (V2, V4), (V1,
V3) are edges.
REPRESENTATION OF GRAPH
Graph can be represented by Adjacency Matrix and Adjacency list.
One simple way to represents a graph is Adjacency Matrix.
The adjacency Matrix A for a graph G = (V, E) with n vertices is an n x n matrix, such that
Aij = 1, if there is an edge Vi to Vj
Aij = 0, if there is no edge.
Adjacency Matrix For Directed Graph

V1 V2 V3 V4
V1 V2 V1 0 1 1 0
V2 0 0 0 1

V3 0 1 0 0
V3 V4
V4 0 0 1 0
Example V1,2 = 1 Since there is an edge V1 to V2
Similarly V1,3 = 1, there is an edge V1 to V3
V1,1 & V1,4 = 0, there is no edge.
Adjacency Matrix For Undirected Graph
V1 V2 V3 V4
V1 V2 V1 0 1 1 0
V2 1 0 1 1

V3 1 1 0 1
V3 V4
V4 0 1 1 0

Adjacency Matrix For Weighted Graph


To solve some graph problems, Adjacency matrix can be constructed as
Aij = Cij, if there exists an edge from Vi to Vj
Aij = 0, if there is no edge & i = j

If there is no arc from i to j, Assume C[i, j] =  where i  j .

V1 V2 V3 V4
3
V1 V2 V1 0 3 9

9 1 7 V2 0 7

V3 1 0
V3 V4
8 V4 1 8 0

Advantage

* Simple to implement.
Disadvantage
* Takes O(n2) space to represents the graph
* It takes O(n2) time to solve the most of the problems.
Adjacency List Representation
In this representation, we store a graph as a linked structure. We store all vertices in a list
and then for each vertex, we have a linked list of its adjacency vertices
Adjacency List

1 2 3 4
2 4 5
3 6
4 3 5 6 7
5 7
6
7 6

BASIC TERMINOLOGIES
Directed Graph (or) Digraph
Directed graph is a graph whichconsists of directed edges, where each edge in E is
unidirectional. It is also referred as Digraph. If (v, w) is a directed edge then (v, w) # (w, v)

V1 V2

(V1, V2)  (V2, V1)


V3

Undirected Graph
An undirected graph is a graph, which consists of undirected edges. If (v, w) is an undi-
rected edge then (v,w) = (w, v)

V1 V2

(V1, V2) = (V2, V1)


V3

Weighted Graph
A graph is said to be weighted graph if every edge in the graph is assigned a weight or
value.
It can be directed or undirected graph.
V1 V2 V1 V2

V3 V3

Complete Graph
A complete graph is a graph in which there is an edge between every pair of vertics. A
complete graph with n vertices will have n (n - 1)/2 edges.

V1 V2 V1 V2

V3 V4 V3 V4

Number of vertics is 4
Number of edges is 6
(i.e) There is a path from every vertex to every other vertex.
A complete digraph is a strongly connected graph.
Strongly Connected Graph
If there is a path from every vertex to every other vertex in a directed graph then it is said
to be strongly connected graph. Otherwise, it is said to be weakly connected graph.

V1
V1

V2 V3 V3 V2

Path
A path in a graph is a sequence of vertices
1 ,2 , such that i ,i1  E for
.................
n
1  i  N . Referring the Fig. 4.3.7 the path from V1 to V3 is V1, V2, V3.
Length
The length of the path is the number of edges on the path, which is equal to N-1, where N
represents the number of vertices.
The length of the above path V1 to V3 is 2. (i.e) (V1, V2), (V2, V3).
If there is a path from a vertex to itself, with no edges, then the path length is 0.
Loop

If the graph contains an edge (v, v) from a vertex to itself, then the path is referred to as a
loop.
Simple Path
A simple path is a path such that all vertices on the path, except possibly the first and the
last are distinct.
A simple cycle is the simple path of length atleast one that begins and ends at the same
vertex.
Cycle
A cycle in a graph is a path in which first and last vertex are the same.

1 2

A graph which has cycles is referred to as cyclic graph.


Degree
The number of edges incident on a vertex determines its degree. The degree of the vertex
V is written as degree (V).
The indegree of the vertex V, is the number of edges entering into the vertex V.
Similarly the out degree of the vertex V is the number of edges exiting from that vertex V.

In fig.
V1 V2

V3 V4

Indegree (V1) = 2
Outdegree (V1) = 1
ACyclic Graph
A directed graph which has no cycles is referred to as acyclic graph. It is abbreviated as
DAG.
DAG - Directed Acyclic Graph.
A

B C

D
E

GRAPH TRAVERSAL
A graph traversal is a systematic way of visiting the nodes in a specific order. There are
two types of graph traversal namely,
 Depth first traversal
 Breadth first traversal
Breadth First Traversal
Breadth First Search (BFS) of a graph G starts from an unvisited vertex u. Then all
unvisited vertices vi adjacent to u are visited and then all unvisited vertices wj adjacent to
vi are visited and so on. The traversal terminates when there are no more nodes to visit.
Breadth first search uses a queue data structure to keep track of the order of nodes whose
adjacent nodes are to be visited.
Steps to implement breadth first search
Step 1: Choose any node in the graph, designate it as the search node and mark it
as visited.
Step 2: Using the adjacency matrix of the graph, find all the unvisited adjacent nodes
to the search node and enqueue them into the queue Q.
Step 3: Then the node is dequeued from the queue. Mark that node as visited and
designate it as the new search node.
Step 4: Repeat step 2 and 3 using the new search node.
Step 5: This process continues until the queue Q which keeps track of the adjacent
nodes is empty.
Routine for breadth first search
Void BFS (vertex u)
{
Initialize queue Q;
visited [u] = 1;
Enqueue (u, Q);
while (! Isempty(Q))
{
u = Dequeue (Q);
print u;
for all vertices v adjacent to u do
if (visited [v] = = 0) then
{
Enqueue (v, Q)
visited [v] = 1;
}
}
}

Example:

A B

Adjacency matrix

C D

A B C D

A 0 1 1 0

B 1 0 1 1

C 1 1 0 1

D 0 1 1 0
Implementation
1. Let ‘A’ be the source vertex. Mark it to as visited.

2. Find the adjacent unvisited vertices of ‘A’ and enqueue then into the queue.
Here B and C are adjacent nodes of A

B C ........
.
and B and C are enqueued.

3. Then vertex ‘B’ is dequeued and its adjacent vertices C and D are taken
from the adjacency matrix for enqueuing. Since vertex C is already in the
queue, vertex D alone is enqueued.

C D ........
.
Here B is dequeued, D is enqueued.

4. Then vertex ‘C’ is dequeued and its adjacent vertices A, B and D are found
out. Since vertices A and B are already visited and vertex D is also in the
queue, no enqueue operation takes place.

D .........

Here C is dequeued

5. Then vertex ‘D’ is dequeued. This process terminates as all the vertices
are visited and the queue is also empty.

B C

Applications of breadth first search


1. To check whether the graph is connected or not.
DEPTH FIRST SEARCH

Depth first works by selecting one vertex V of G as a start vertex ; V is marked visited.
Then each unvisited vertex adjacent to V is searched in turn using depth first search
recursively. This process continues until a dead end (i.e) a vertex with no adjacent unvisited
vertices is encountered. At a deadend, the algorithm backsup one edge to the vertex it came
from and tries to continue visiting unvisited vertices from there.

The algorithm eventually halts after backing up to the starting vertex, with the latter being
a dead end. By then, all the vertices in the same connected component as the starting vertex
have been visited. If unvisited vertices still remain, the depth first search must be restarted at
any one of them.

To implement the Depthfirst Search perform the following Steps :

Step : 1 Choose any node in the graph. Designate it as the search node and mark it as
visited.
Step : 2 Using the adjacency matrix of the graph, find a node adjacent to the search.
node that has not been visited yet. Designate this as the new search node and
mark it as visited.
Step : 3 Repeat step 2 using the new search node. If no nodes satisfying (2) can be
found, return to the previous search node and continue from there.
Step : 4 When a return to the previous search node in (3) is impossible, the search from
the originally choosen search node is complete.
Step : 5 If the graph still contains unvisited nodes, choose any node that has not been
visited and repeat step (1) through (4).

ROUTINE FOR DEPTH FIRST SEARCH


Void DFS (Vertex V)

visited [V] = True;

for each W adjacent to V

if (! visited [W])

Dfs (W);

}
Example : -

A B

C D

Adjacency Matrix
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 1 0
Implementation
1. Let ‘A’ be the source vertex. Mark it to be visited.
2. Find the immediate adjacent unvisited vertex ‘B’ of ‘A’ Mark it to be visited.
3. From ‘B’ the next adjacent vertex is ‘d’ Mark it has visited.
4. From ‘D’ the next unvisited vertex is ‘C’ Mark it to be visited.

Depth First Spanning Tree


Applications of Depth First Search
1. To check whether the undirected graph is connected or not.
2. To check whether the connected undirected graph is Bioconnected or not.
3. To check the a Acyclicity of the directed graph.

Undirected Graphs
A undirected graph is ‘connected’ if and only if a depth first search starting from any node
visits every node.

B D E

An Undirected graph
Adjacency Matrix
A B C D E
ABCDE 0 1 0 1 1
1 0 1 1 0
Implementation
0 1 0 1 1
We start at vertex ‘A’. Then Mark A as visited and call DFS (B) recursively, Dfs (B)
1 calls
Marks B as visited and 1 Dfs(c)
1 recursively.
0 0
1 0 1 0 0
A

B
Dfs (c) marks C as visited and calls Dfs (D) recursively. No recursive calls are made to Dfs
(B) since B is already visited.

Dfs(D) marks D as visited. Dfs(D) sees A,B,C as marked so no recursive call is made
there, and Dfs(D) returns back to Dfs(C).

B A

C B

D C

Dfs(C) calls Dfs(E), where E is unseen


D
E

adjacent vertex to C.
Since all the vertices starting from ‘A’ are visited, the above graph is said to be
connected. If the graph is not connected, then processing all nodes requires reversal calls to Dfs,
and each generates a tree. This entire collection is a depth first spanning forest.
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 Vj, then Vj appears after Vi in the linear ordering.
Topological ordering is not possible. If the graph has a cycle, since for two vertices v and
w on the cycle, v precedes w and w precedes v.
To implement the topological sort, perform the following steps.
Step 1 : - Find the indegree for every vertex.
Step 2 : - Place the vertices whose indegree is ‘0’ on the empty queue.
Step 3 : - Dequeue the vertex V and decrement the indegree’s of all its adjacent
vertices.
Step 4 : - Enqueue the vertex on the queue, if its indegree falls to zero.
Step 5 : - Repeat from step 3 until the queue becomes empty.
Step 6 : - The topological ordering is the order in which the vertices dequeued.
Routine to perform Topological Sort
/* Assume that the graph is read into an adjacency matrix and that the indegrees are
computed for every vertices and placed in an array (i.e. Indegree [ ] ) */
void Topsort (Graph G)
{
Queue Q ;
int counter = 0;
Vertex V, W ;
Q = CreateQueue
(NumVertex); Makeempty (Q);
for each vertex V
if (indegree [V] = = 0)
Enqueue (V, Q);
while (! IsEmpty (Q))
{
V = Dequeue (Q);
TopNum [V] = + + counter;
for each W adjacent to V
if (--Indegree [W] = = 0)
Enqueue (W, Q);
}
if (counter ! = NumVertex)
Error (“ Graph has a cycle”);
DisposeQueue (Q); /* Free the Memory */
}

Note :
Enqueue (V, Q) implies to insert a vertex V into the queue
Q. Dequeue (Q) implies to delete a vertex from the queue Q.
TopNum [V] indicates an array to place the topological numbering.
Example 1 :

a a b c d
a 0 1 1 0
b 0 0 1 1
b c
c 0 0 0 1
0 0 0 0
d d

Fig. Adjacency Matrix


Step 1
Number of 1’s present in each column of adjacency matrix represents the indegree of the
corresponding vertex.
In fig 4.6.1 Indegree [a] = 0 Indegree [b] = 1
Indegree [c] = 2 Indegree [d] = 2
Step 2
Enqueue the vertex, whose indegree is ‘0’
In fig 4.6.1 vertex ‘a’ is 0, so place it on the queue.
Step 3
Dequeue the vertex ‘a’ from the queue and decrement the indegree’s of its adjacent
vertex ‘b’ & ‘c’
Hence,Indegree [b] = 0 and Indegree [c] = 1
Now, Enqueue the vertex ‘b’ as its indegree becomes zero.
Step 4
Dequeue the vertex ‘b’ from Q and decrement the indegree’s of its adjacent vertex ‘c’ and
‘d’. Hence, Indegree [c] = 0 and Indegree [d] = 1
Now, Enqueue the vertex ‘c’ as its indegree falls to zero.
Step 5
Dequeue the vertex ‘c’ from Q and decrement the indegree’s of its adjacent vertex
‘d’. Hence, Indegree [d] = 0
Now, Enqueue the vertex ‘d’ as its indegree falls to zero.
Step 6
Dequeue the vertex ‘d’.
Step 7
As the queue becomes empty, topological ordering is performed, which is nothing but,
the order in which the vertices are dequeued.

VERTEX 1 2 3 4
a 0 0 0 0
b 1 0 0 0
c 2 1 0 0
d 2 2 1 0
ENQUEUE a b c d
DEQUEUE a b c d

RESULT OF APPLYING TOPOLOGICAL SORT TO THE GRAPH IN FIG. 4.6.1


Example 2 :

V1 V2

V3 V4 V5

V6 V7

Fig
Adjacency Matrix :-

V1 V2 V3 V4 V5 V6 V7
V1 0 1 1 1 0 0 0
V2 0 0 0 1 1 0 0
V3 0 0 0 0 0 1 0
V4 0 0 1 0 0 1 1

V5 0 0 0 1 0 0 1

V6 0 0 0 0 0 0 0
0 0 0 0 0 1 0
V7
0 1 2 3 1 3 2
INDEGREE

Indegree [V1] = 0 Indegree [V2] = 1 Indegree [V3] = 2


Indegree [V4] = 3 Indegree [V5] = 1 Indegree [V6] = 3
Indegree [V7] = 2
INDEGREE BEFORE DEQUEUE #
VERTEX 1 2 3 4 5 6 7
V1 0 0 0 0 0 0 0
V2 1 0 0 0 0 0 0
V3 2 1 1 1 0 0 0
V4 3 2 1 0 0 0 0
V5 1 1 0 0 0 0 0
V6 3 3 3 3 2 1 0
V7 2 2 2 1 0 0 0
ENQUEUE V1 V2 V5 V4 V 3, V7 V6
DEQUEUE V1 V2 V5 V4 V3 V7 V6
RESULT OF APPLYING TOPOLOGICAL SORT TO THE GRAPH IN FIG. 4.6.2
The topological order is V1, V2, V5, V4, V3, V7, V6
Analysis
The running time of this algorithm is 0  E + V  . where E represents the Edges & V
represents the vertices of the graph.
BICONNECTIVITY
A connected undirected graph is biconnected if there are no vertices whose removal
discon- nects the rest of the graph.
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
socialstructures. An example is the twitter graph of who follows whom. These can be
used to determine how information flows, how topics become hot, how communities
develop, or even who might be a good match for who, or is that whom.
2. Transportation networks. In road networks vertices are intersections and edges are the
road segments between them, and for public transportation networks vertices are stops
and edges are the links between them. Such networks are used by many map programs
such as Google maps, Bing maps and now Apple IOS 6 maps (well perhaps without the
public transport) to find the best routes between locations. They are also used for studying
traffic patterns, traffic light timings, and many aspects of transportation.
3. Utility graphs. The power grid, the Internet, and the water network are all examples of
graphs where vertices represent connection points, and edges the wires or pipes between
them. Analyzing properties of these graphs is very important in understanding the
reliability of such utilities under failure or attack, or in minimizing the costs to build
infrastructure that matches required demands.
4. Document link graphs. The best known example is the link graph of the web, where
each web page is a vertex, and each hyperlink a directed edge. Link graphs are used, for
example, to analyze relevance of web pages, the best sources of information, and good
link sites.
5. 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, for example, to study molecular pathways—chains of molecular
interactions in a cellular process. Humans have over 120K proteins with millions of
interactions among them.
6. 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.
7. Scene graphs. In graphics and computer games scene graphs represent the logical or
spacial relationships between objects in a scene. Such graphs are very important in the
computer games industry.
8. Finite element meshes. In engineering many simulations of physical systems, such as the
flow of air over a car or airplane wing, the spread of earthquakes through the ground, or
the structural vibrations of a building, involve partitioning space into discrete elements.
The elements along with the connections between adjacent elements forms a graph that is
called a finite element mesh.
9. Robot planning. Vertices represent states the robot can be in and the edges the possible
transitions between the states. This requires approximating continuous motion as a
sequence of discrete steps. Such graph plans are used, for example, in planning paths for
autonomous vehicles.
10.Neural networks. Vertices represent neurons and edges 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

You might also like