0% found this document useful (0 votes)
6 views

Graph

Uploaded by

Raghu M E
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Graph

Uploaded by

Raghu M E
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Graph

Contents

 Introduction
 Basic concepts
 Graph terminologies
 Graph Implementation
 Using Adjacent matrix
 Using path matrix
 Using adjacency list
 Shortest path algorithm
 Graph traversal
 Breadth First Search (BSF)
 Depth First Search (DFS)
Introduction

 One of the Data structure.


 Similar to mathematical graph
 Consist of vertices and connected by edges.
 Operations – finding path between nodes, shortest possible path.
 Application – Computer network analysis, travel application , chip designing, gaming, etc..
Basic concepts.

 G=(V, E)
 V={ v1, v2, v3, …… Vn} set of vertices or nodes
 E= {E1,E2,E3,…….Em} set of edges or arcs

e1

V1 V3
e2 e3 e5
V5

V2 V4 e4
e=(u,v)
 Un directed graph.
 Directed graph or digraph
 G(V)={v1,v2,v3,v4,v5}
 G(E)={v1.v2),(v1,v3),(v3,v4)(v4,v2)(v3,v5)}

V1 V3

V5

V2 V4
Graph Terminologies.
 Key term Description
1. Adjacent node if e(u,v) then u and v are adjacent to each other
2. Predecessor node if e(u,v) in digraph, u is predecessor of v
3. Successor node if e(u,v) in digraph, , v is successor node of u
4. Degree number of edges connected to node or vertex
5. In degree Number of edges ending at the vertex.
6. Out degree Number of edges beginning at the vertex.
7. Path Sequence of vertices each adjacent to the next.
8. Cycle it is the path starts and ends at same vertex.
9. Loop an edge ends at same vertex (u,u).
10. Weight Non negative number assigned to each edge. (length)
11. Order Number of vertices in the graph.
12. Labelled graph graph with labelled edges.
13. Weighted graph weights assigned to each edges.
15. Strongly connected graph Directed graph where route between each node
16. Complete graph undirected graph, where direct edges between
each vertices.
17. Tree. Connected graph without
cycle. (No cycle).
Graph Implementation

 Graph –G is collection of nodes and edges.


 Capture details of vertices (nodes) and edges.
 3 different methods.
 Adjacency matrix
 Path matrix
 Adjacency List
Graph implementation using Adjacency matrix

 G=(V,E). Having N nodes.


 Matrix contains N*N order.
 Ai,j = 1 if there is an edge between vertex Vi to Vj
 Ai,j = 0 if there is no edge between vertex Vi to Vj
1 2 3 4 5
2 3
1 1 1 0 1 0
5
2 0 0 1 0 0

1 4 3 0 0 0 0 1

4 0 0 1 0 0

5 0 0 0 0 0
Program
#include<stdio.h>
#include<conio.> printf(“The Adjacency matrix is \n”);
for(i=0;i<5;i++)
void main() {
{ for(j=0;j<5;j++)
int I, j, A[5][5]; {
printf(“%d\t”,A[i][j);
}
/*initialize matrix*/ printf(“\n”);
for(i=0;i<5;i++) }
}
for(j=0;j<5;j++)
A[i][j]=0;
A[0][0]=1;
A[0][1]=1;
A[0][1]=1;
A[1][2]=1;
A[2][4]=1;
A[3][2]=1;
Advantages and Disadvantages

 Advantage
 Very simple representation/Implementation.

 Dis-Advantages
 Require O(n2) memory space.
 Lot of memory will be wasted, Inefficient for graph with large number of vertices.
Implementation using path matrix

 G=(V,E)
 Matrix contains N*N order.
 Pi,j = 1 if there is an edge between vertex Vi to Vj
 Pi,j = 0 if there is no edge between vertex Vi to Vj
 Now the path matrix P can be deduced using adjacency matrix of G.
 PN= A + A2 + A3 + A4 ……. + AN All non-zero entries are resulting from addition
operation are replaced by 1 to arrive to path matrix.
 But it is very in efficient, requires more number of matrix multiplications.
 Warshall has suggested more efficient method.
 Warshall method
 Identify the direct path between nodes V i to Vj
 Identify an indirect path between nodes V i and Vj, that is path from Vi to Vk and Vk to Vj

Pi,j= Pi,j OR (Pi,k AND Pk,j)


Warshall Algorithm

Step 1 : start
Step 2 : set p[] = A[];
Step 3 : set i=j=k= 1
Step 4 : Repeat through steps 5-10 while k<=N
Step 5 : Repeat through steps 6-9 while i<=N
Step 6 : Repeat through steps 7-8 while j<=N
Step 7 : P[i,j] = P[i,j] or (P[i,k] and P[k,j]);
Step 8 : j= j+1;
Step 9 : i = i+1;
Step 10: k=k+1;
Step 11: display path matrix.
Step 12: stop.
Program
#include<stdio.h>
#include<conio.> printf(“The Adjacency matrix is \n”);
void main() for(i=0;i<5;i++)
{
{
for(j=0;j<5;j++)
int I, j,k, A[5][5],P[5][5]; {
printf(“%d\t”,A[i][j);
}
/*initialize matrix*/ printf(“\n”);
for(i=0;i<5;i++) }
/*assign A to P*/
for(j=0;j<5;j++) for(i=0;i<5;i++)
A[i][j]=0; for(j=0;j<5;j++)
P[i][j]=A[i][j];
A[0][0]=1;
A[0][1]=1; /*create path matrix*/
A[0][1]=1; For(k=0;k<5;k++)
for(i=0;i<5;i++)
A[1][2]=1; for(j=0;j<5;j++)
A[2][4]=1; P[i][j]= (P[i][j] ||(P[i][k]&&P[k][j]));

A[3][2]=1;
printf(“The Path matrix is \n”);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf(“%d\t”,P[i][j);
}
printf(“\n”);
}
}
Adjacency List representation
struct vertex
{
int data;
struct vertex * edge[10];
} node[10];

node[i].edge[j]=&node[j];
Shortest path algorithm

 One of the common problem associated with graph is – finding shortest path .
Wi,j 1 2 3 4 5

1 8 3 0 4 0
7
2 3 5 2 0 0 7 0 0

3 4 0 0 0 5
3 5
4 2
4 0 0 2 0 0
1 4 1 5 0 0 0 1 0
4
8

Weighted digraph
After appling warshellSPi,j =minimum of (SPi,j, SPi,k + SP )
k,j

SPi,j 1 2 3 4 5 SPi,j 1 2 3 4 5

1 8 3 8 4 8 1 8 3 6 4 11

2 8 8 7 8 8 2 11 14 7 13 12

3 4 8 8 8 5 3 4 7 8 6 5

4 8 8 2 8 8 4 6 9 2 8 7

5 8 8 8 1 8 5 7 10 3 1 8
Modified Warshall Algorithm
Step 1 : start
Step 2 : set i=j= 1
Step 3 : Repeat through steps 4-9 while i<=N
Step 4 : Repeat through steps 5-8 while j<=N
Step 5 : if P[i,j]=0 goto step 6 else step 7
Step 6: set SP[i,j]=8;
Step 7 : Set SP[i,j]=P[i,j].
Step 8 : j=j+1
Step 9 : i = i+1;
Step 10 : set i=j=k= 1
Step 11:Repeat through steps 12-17 while K<=N
Step 12 : Repeat through steps 13-16 while j<=N
Step 13 : Repeat through steps 12-15 while i<=N
Step 14 : SP[i,j] = MINIMUM (SP[i,j] , (SP[i,k] + SP[k,j]);
Step 15 : j= j+1;
Step 16 : i = i+1;
Step 17: k=k+1;
Step 18: display path matrix SP.
Step 20: stop.
Shortest path using modified Warshell
#include<stdio.h> printf(“The Path matrix is \n”);
#include<conio.> for(i=0;i<5;i++)
{
int MIN(int, int);
for(j=0;j<5;j++)
void main() {
{ printf(“%d\t”,P[i][j);
int I, j,k, SP[5][5],P[5][5]; }
printf(“\n”);
}
/*initialize matrix*/
for(i=0;i<5;i++) for(i=0;i<5;i++)
for(j=0;j<5;j++) for(j=0;j<5;j++)
if (P[i][j]==0)
P[i][j]=0;
SP[i][j]=999;
A[0][0]=8; else
A[0][1]=3; SP[i][j]= P[i][j];
A[0][3]=4;
for(k=0;k<5;k++)
for(i=0;i<5;i++)
A[1][2]=7;
for(j=0;j<5;j++)
A[2][4]=5; SP[i][j]= MIN(SP[i][j],(SP[i][k]+SP[k]
A[3][2]=2; [j]));
A[4][3]=1;
printf(“The Shortest Path matrix is \n”);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf(“%d\t”,SP[i][j);
}
printf(“\n”);
}
}
int MIN (int x,int y)
{
if (x<y)
return x;
Else
return y;
}
Graph Traversal
 Visiting graph nodes and edges in systematic way
 2 methods.
 Breadth First Search (BFS)
 Depth First Search (DFS)
 Both method consider graph nodes in any one of following state
 Ready state.
 Waiting state.
 Processed state.
 The state of the node keeps on changing as the graph traversal continues. Once the node is
in processed state, then node is considered as visited
Breadth First Search (BFS)

 Begin by processing starting node.


 Progresses with adjacent nodes or neighbouring nodes.
 Once the all neighbouring nodes of start nodes are analysed, algorithm stars analysing
neighbouring nodes of each node.
 This method requires frequent backtracking to already analysed nodes.
 Requires data structure.
 Queue is the data structure used by BFS.
v4

v2 v8

v5
v1

v7
v3

v6
 V1, V2,V3,V4,V5,V6,V7,V8
 V1,V3,V2,V6,V5,V4,V7,V8
BFS algorithm.
BFS (adj[], status[], queue[], N)
Step 1: start
Step 2: set status[]=1;
Step 3: Insert (queue, V1);
Step 4: set status[v1]=2;
Step 5 : repeat step 6-11 while queue[] not empty
Step 6: V= delete (queue[])
Step 7: set status[V]=3;
Step 8: Repeat step 9-11 while adj(V) is not empty.
Step 9: if adj(V)=1 goto step 10 else goto step 8.
Step 10: insert (queue, adj(V)).
Step 11: set adj(V)=2.
Step 12: stop
DFS

 Visit the graph nodes level by level.


 Visits in different paths.
 Begins analysis from start node to the end node, then proceeds along the next path from
start node.
 Repeated until graph nodes are visited.
 Need back tracking , data structure used is stack.
v4

v2 v8

v5
v1

V7
v3

v6

 Paths are V1,V2,V4,V8,V5,V7,V3,V6


 Another path V1,V3,V6,V7,V8,V2,V5,V4
DFS algorithm.
DFS (adj[], status[], stack[], N)

Step 1 : start
Step 2: set status[]=1;
Step 3: PUSH( stack, V1)
Step 4: set status[V1]=2;
Step 5: repeat step 6-11 while stack[] is not empty
Step 6: V= POP (stack[])
Step 7: set status[V]=3;
Step 8: Repeat step 9-11 while adj(V) is not empty.
Step 9: if adj(V)=1 goto step 10 else goto step 8.
Step 10: PUSH (stack, adj(V)).
Step 11: set adj(V)=2.
Step 12: stop

You might also like