0% found this document useful (0 votes)
93 views22 pages

Unit V Graphs: Definition of Graph

Graphs are classified in to two types 1. Directed graphs 2. Un directed graphs in the directed graph the directions are shown on the edges. In an undirected graph the edges are not ordered. In this type of graph the edge E1 is the set of (V1,V2) or (V2,V1).

Uploaded by

msse89
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views22 pages

Unit V Graphs: Definition of Graph

Graphs are classified in to two types 1. Directed graphs 2. Un directed graphs in the directed graph the directions are shown on the edges. In an undirected graph the edges are not ordered. In this type of graph the edge E1 is the set of (V1,V2) or (V2,V1).

Uploaded by

msse89
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Generated by Foxit PDF Creator © Foxit Software

http://www.foxitsoftware.com For evaluation only.

Unit V
Graphs
Definition of graph
A graph is a collection of two sets V and E where V is a finite non empty set
of vertices and E is a finite non empty set of edges.
Vertices are nothing but the nodes in the graph and the two adjacent vertices
are joined by edges. The graph is thus a set of two sets. Any graph G is
denoted by G = {V,E}
Types of graph:
Basically graphs are classified in to two types
1. Directed graphs
2. Un directed graphs
In the directed graph the directions are shown on the edges. As shown in the
following figure the edges between the vertices are ordered.
V1
E1
E2

V2 3

E3 V4
E4

Directed Graph

In this type of graph, the edge E1 is in between vertices V1 and V2. the V1
is called head and the V2 is called the tail. Similarly for V1 head the tail is
V3 and so on.
We can say E1 is the set of (V1,V2) and not of (V2,V1).

1
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Similarly, in an undirected graph, the edges are not ordered. In this type of
Graph the edge E1 is the set of (V1,V2) or (V2,V1).

V1
E1

V2 V3

V4

Properties of Graph:
Complete graph:
If an undirected graph of n vertices consists of n(n-1)/2 number of edges
then it is called as complete graph.
The graph shown in following figure is a complete graph.
V1 V2

V4 V3

Complete graph
Sub Graph:
A sub graph G’ of graph is a graph such that the set of vertices and set of
edges of G’ are proper subset of the set of edges of G.
V1 V1

V2 V3

V2
G G’

2
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Connected Graph:
An undirected graph is said to be connected if for every pair of distinct
vertices Vi and Vj in V(G) there is a graph from Vi to Vj in G.

V1

V2 V3

V4

Representation of Graphs:
There are several representation of graph, but we will discuss the two
commonly used representations called
 Adjacency matrix
 Adjacency lists
Adjacency matrix representation:
The adjacency matrix of G is a 2-dimensional array of size n*n ( where n is
the number of vertices in the graph). Consider a graph G of n vertices and
the matrix M. if there is an edge present between vertices Vi and Vj then
M[I][j]=1 else M[I][j]=0. note that for an undirected graph if M[I][j]=1 then
M[j][I] is also 1. Here are some graphs shown by adjacency matrix.
1 2 3 4
V1
1 0 1 1 0
2 0 0 0 0
V2 V3
3 0 0 0 0

4 0 1 1 0
V4

3
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

1 2 3 4
V1
1 0 1 1 0

2 1 0 0 1
V2 V3
3 1 0 0 1

V4 4 0 1 1 0

As can be seen from above, the adjacency matrix for an undirected graph is
symmetric. The adjacency matrix for a directed graph need not be
symmetric. The space needed to represent a graph using its adjacency matrix
is n2 locations. About half this space can be saved in the case of undirected
graphs by storing only the upper or lower triangle of the matrix.

Adjacency Lists:
In this representation the n rows of the adjacency matrix are
represented as n linked lists. There is one list for each vertex in the graph.
The nodes in list I represent the vertices that are adjacent from vertex i. Each
list has a head node.

4
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

The head nodes are sequential providing easy random access to the
adjacency list for any particular vertex. The adjacency lists for graphs G1
and G2 are shown below.

Vertex 1 2 3 4 N

Vertex 2 1 3 4 N 5 N

Vertex 3 1 2 4 N

Vertex 4 1 2 3 5 N

Vertex 5 2 4 N

(a) Adjacency lists for G1

Vertex 1 2 N

Vertex 2 1 3 N

Vertex 3 2 N

(b) Adjacency lists for G2

Adjacency lists.

Given the root node of a binary tree, one of the most common operations
performed is visiting every node of the tree in some order. Similarly, given a
vertex in a directed or undirected graph we may wish to visit all vertices in
two ways_ using the Depth First Search and the Breadth First Search
algorithm.

5
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Depth First Search:

The algorithm for Depth first search of an undirected


graph is as follows:
(a) Visit is a vertex, say v. Mark this vertex as visited.
(b) Select an unvisited vertex w adjacent to v.
(c) Repeat steps (a) and (b) till all adjacent vertices of w are visited.
(d) On reaching a vertex adjacent to it and go back to step (a).
(e) Terminate the search when no unvisited vertex can be reached from
any of the visited ones.
Explanation of logic for Depth first traversal:

2 3

In DFS the basic data structure for storing the adjacent nodes is stack. In the
algorithm we have used a recursive call to DFS function. When a recursive
call is invoked actually push operations get performed. When we exit form
the loop pop operation will be performed. Let us see how our algorithm
works.
Step1: start with vertex 1, print it. So ‘1’ gets printed. Mark 1 as visited.

Visited
Output screen
0 0
1
1 1

2 0

3 0
6
4 0
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Step2: Find adjacent vertex to 1, say i.e. 2 if it is not visited, 2 will get
inserted in the stack, mark it as visited.

Visited Stack
Output screen
0 0 2 Top After exiting the
loop 2 will be 1 2
1 1 popped print 2

2 1

3 0

4 0

Step3: Find adjacent to 2 i.e. vertex 4 if it is not visited, 4 will get pushed on
the stack mark it as visited.

Visited Stack
Output screen
0 0 4 Top After exiting the
loop 4 will be 1 2 4
1 1 popped print 4

2 1

3 0

4 1

Step4: Find adjacent to 4 i.e. vertex 3 if it is not visited, 3 will be pushed


onto the stack mark it as visited.

Visited Stack
Output screen
0 0 3 Top After exiting the
loop 3 will be 1 2 4 3
1 1 popped print 3

2 1

3 1
Since all the nodes are coverer stop the procedure.
4 1 So output of DFS is: 1 2 4 3

7
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Breadth First Traversal of a Graph


Algorithm:
1. Create a graph. Depending on the type of graph i.e. directed or
undirected set the value of the flag either 0 or 1 respectively.
2. Read the vertex from which you want to traverse the graph say Vi
3. Initialize the visited array to 1 at the index of Vi.
4. Insert the visited vertex Vi in the queue.
5. Visit the vertex which is at the front of the queue. Delete it from the
queue and place its adjacent nodes in the queue.
6. Repeat the step 5, till the queue is not empty.
7. Stop.
Explanation of logic of BFS program

2 3

In BFS queue is maintained for storing the adjacent nodes and an array
‘visited’ is maintained for keeping the track of visited nodes. i.e., once a
particular node is visited it should not be revisited again.

8
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Step 1: Start with vertex 1

Visited Queue
-1 0 1 2 3 4
0 0
Inserted vertex 1 in queue
1 1 and marked the index 1 of
visited array by 1
2 0
Front Rear
3 0

4 0

Step 2:
Output Screen

0 1 2 3 4 Delete 1 and 1
print it. So 1
gets printed

Front
Rear
Step 3: Find adjacent vertices of vertex 1 and mark them as visited, insert
those in queue. The adjacent vertices of vertex 1 are 2 and 3.
Visited

0 0 0 1 2 3 4
Increment front by 1 delete
1 1 1 2 3 2 from queue and print it so
2 gets printed
2 1
Font Rear
3 1

4 0

9
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Step 4: Find adjacent to 2 and insert those nodes in queue as well as mark
them as visited.

Visited
Output Screen
0 0 0 1 2 3 4
1 2
1 1 1 2 3 4

2 1
Font Rear
3 1

4 1

Step 5: Increment front and delete the node print it.

Output Screen
0 1 2 3 4
1 2 3
1 2 3 4 So 3 gets
printed

Font Rear

Step 6: find adjacent to 3 i.e. 4 check whether it is marked as visited. If it is


marked as visited don’t insert in the queue.

Visited
Output Screen
0 0 0 1 2 3 4
1 2 3 4
1 1 1 2 3 4

2 1
Front
3 1 Rear

4 1

Increment front, delete the node form the queue and print it.
So output will be- BFS for above graph is: 1 2 3 4

10
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Applications of Graph
1. In computer networking such as local area network (LAN), wide area
networking (WAN), internetworking.
2. In telephone cabling graph theory is effectively used.
3. In job scheduling algorithms, graphs are used.
Since graph is nothing but the collection of nodes (vertices) and edges. One
can find the best-suited distance within the vertices. If the distance between
the two vertices is reduced then the cost of cabling and the time in traveling
through the nodes will get reduced. .
Definition of spanning tree
A spanning tree is a subset of a tree T in which all the vertices of tree T are
present but it may not contain all the edges.
The minimum spanning tree of a weighted connected graph G is called
minimum spanning tree if its weight is minimum. There are two popular
algorithms are used to construct minimum spanning tree, namely prim’s
algorithm and kruskal’s algorithm.
1. Prim’s algorithm: in prim’s algorithm the pair with the minimum
weight is to be chosen. Then adjacent to these vertices which ever is
the edge having minimum weight is selected. This process will be
continued till all the vertices are not been covered.
For example consider the following graph:
V1 2 V2

4 1 3 3

V3 V4 V5
2 7
5 8 4
1 6
V6 V7

11
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Prim’s algorithm can be explained with the help of prim’s table. The initial
configuration is shown below.
V Known dv pv
V1 0 0 0
V2 0  0
V3 0  0
V4 0  0
V5 0  0
V6 0  0
V7 0  0

For each vertex we keep values dv and pv and an indication of whether it is


known or unknown. Dv is the weight of the shortest arc connecting to v to a
known vertex, and pv, as before, is the last vertex to cause a change in dv.
Initially v1 is selected and v2,v3,v4 are updated. The table shown below
after v1 is declared as known vertex.
V Known dv pv
V1 1 0 0
V2 0 2 V1
V3 0 4 V1
V4 0 1 V1
V5 0  0
V6 0  0
V7 0  0

The table after v1 is declared known

12
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

The next vertex selected is v4. Every vertex adjacent to v4. v1 is not
examined because it is known. V2 is unchanged, because it has dv = 2 and
the edge cost from v4 to v2 is 3; all the rest are updated. This shown in the
below table.
V Known dv pv
V1 1 0 0
V2 0 2 V1
V3 0 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 8 V4
V7 0 4 V4
The table after v4 s declared known
The next vertex chosen is v2. This does not affect any distances. Then v3 is
chosen, which affects the distance in v6, producing results from the selection
of v7, which forces v6 and v5 to be adjusted. Finally V6 and v5 are selected.
V Known dv pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 5 V3
V7 0 4 V4
The table after v2 and then v3 are declared known

13
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

V Known dv pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 0 1 V7
V7 1 4 V4
The table after v7 is declared known
V Known dv pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 0 1 V7
V7 0 4 V4
The table after v6 and v5 are selected ( prim’s algorithm terminates)
The edges in the spanning tree can be read from the table: (v2,v1), (v3,v4),
(v4,v1), (v5,v7), (v6,v7), (v7,v4). The total cost is 16.

V1
2 V2
1

V3 2 V4 V5

4 6
1
V6 V7

14
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Kruskal’s Algorithm: In Kruskal’s algorithm the minimum weight is


obtained. In this algorithm also the circuit should not be formed. Each time
the edge of weight has to be selected, form the graph. It is not necessary in
this algorithm to have edges of minimum weights to be adjacent. Let us
solve one example by Kruskal’s algorithm.

8 1
(1)
2 3 1
13

12 2 8 (2)
7
1
12
2
10 9

(3) (4)
1 8
2 1

2
3 3

Weight = 21

15
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Shortest path algorithm:


By minimum spanning tree, we are not able to obtain the shortest
path between two nodes (source and destination nodes). We can
obtain simply the minimum cost. But by using shortest path
algorithm we can obtain the minimum distance between two nodes.
In our laboratories we have local area network for all the
computers. Before designing LAN we should always find out the
shortest path and thereby we can obtain economical networking.
There are weighted and un-weighted graph. Based on this category, let us
discuss the shortest path algorithm.
1. Un-weighted shortest path: the un-weighted shortest path algorithm
gives a path in un-weighted graph which is equal to number of edges
traveled from source to destination.
For example: consider the graph given below

a z

The paths between a to z are as below:


Sr.No Path Number of edges
1 V1-v2-v3-v10 3
2 V1-v4-v5-v6-10 4
3 V1-v7-v8-v9-v10 4
Out of these the path 1 i.e. v1-v2-v3-v10 is shortest one as it consists of only
3 edges from a to z

16
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

2. Dijkstra’s shortest path algorithm: the Dijkstra’s shortest path


algorithm suggests the shortest path from some source node to the
some other destination node. The source node or the node from we
start measuring the distance is called the start node and the destination
node is called the end node. In this algorithm we start finding the
distance from the start node and find the all the paths from it to
neighboring nodes. Among those which ever is the nearest node that
path is selected. This process of finding the nearest node is repeated
till the end node. Then whichever is the path that path is called the
shortest path.
For example: consider the graph given below

P = set which is for nodes which have already selected


T = remaining node
Step1: v = a
P ={2}, T = {b,c,d,e,f,z}
Dist (b) = min{old dist(b), dist(a)+w(a,b)}
Dist (b) = min{,0+22}
Dist (b) = 22
Dist(c) = 16
Dist (d) = 8 (minimum node)

17
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Dist (e) = 
Dist (f) = 
Dist (z) = 
So minimum node is selected in p i.e. node d
Step 2: v = d
P = {a,d} T = {b,c,e,f,z}
Dist (b) = min{old dist(b),dist(d)+w(b,d)}
Dist (b)=min{22,8+}
Dist (b)=22
Dist(c) =min{16,8+10}=16
Dist (e)=min{,8+}=8
Dist (f)=min{,8+6}=14
Dist (z)=min{,8+}=8
Step 3: v=f
P={a,d,f} T={b,c,e,z}
Dist (b) = min{22,14+7} = 21
Dist(c) = min{16,14+3} = 16
Dist (e) = min{,14+} = 
Dist (z) = min(,14+9} = 23
Step 4: v = c
P = {a,d,f,c} T = {b,e,z}
Dist (b) = min{21,16+20} = 21
Dist (e) = min(,16+4} = 20
Dist (z) = min{23,16+10} = 23

18
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Step 5: v= e
P={a,d,f,c,e} T={b,z}
Dist (b) = min{21,20+2} = 21
Dist (z) = min{23,20+4} = 23
Step 6: v=b
P={a,d,f,c,e,b} T = {z}
Dist (z) = min{23,21+2} = 23.
Now the target vertex for finfing the shortest path is z. hence the length of
the shortest path from the vertex a to z is 23.
The shortest path is as shown below:

Bi-connectivity
Bi-connected graphs are the graphs which can not be broken into two
disconnected pieces (graphs) by connecting single edge. For example

V1 E1 V3 E5

E2 V5
E3

V2 V4 E6
E4

19
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

In the given figure even if we remove any single edge the graph does not
become disconnected.

V1 V3
E5

E2 E3 V5

E6
V2 V4
E4
For example even if we remove an edge E1 the graph does not become
disconnected. We do not get two disconnected components of graph.
Properties of bi-connected graph:
1. There are two disjoint paths between any two vertices
2. There exists simple between two vertices
3. There should not be any cut vertex (cut vertex is a vertex which if we
remove then the graph becomes disconnected)
Introduction to NP completeness problem
For obtaining solution to any problem there are two classes defined

Problem Solution

P class NP class

1. P class (polynomial time class)


P is a class of problems which could be solved deterministically
(definitely) in a polynomial time

20
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

The term polynomial time means the time which is function of


polynomial(x). that means the polynomial time = coef * xi
Where I= 0 to N.
If N=0, then polynomial time = constant
If N=1, then polynomial time=x
If N=2, then polynomial time=x2
And so on that means in sample terms polynomial time is linear time or,
square time or cubic time and so on.
2. NP class (Non deterministic polynomial time class)
NP completeness problem is a class of problems for which we can make
out the guess of solution of these problems which will get solved in
polynomial time.
For example:
Traveling salesman’s problem
The traveling salesman’s problem can be stated as follows. A traveling
salesman wants to traverse a path such that he starts at vertex V0 (we can
even call each vertex as a city) and visits every other vertex exactly once
finally returns to vertex V0. Find out the minimum path length.

V1 V3 V6

V2 V4 V5

21
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

This problem is NP complete problem. For the above graph we can


not deterministically find out solution, such a solution which will
be executable in polynomial time.
Hamiltonian problem: a Hamiltonian problem can be stated as follows. A
graph is posses Hamiltonian cycle if we can traverse it starting from vertex
V0 visits every vertex exactly once and returns to vertex V0.
For example:

1 2 3

8 7 6

The Hamiltonian cycle will be 1-2-8-7-6-5-4-3-1. The Hamiltonian problem


is also NP complete problem. As we can simply make a guess of solution
which will get executed in polynomial time.

22

You might also like