04 Unit DS Notes
04 Unit DS Notes
What is a graph?
Is a data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes
to each other
The set of edges describes relationships among
the vertices
Formal definition of graphs
A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of
vertices
E(G): a set of edges (pairs of vertices)
Directed vs. undirected graphs
1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graph G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={(0,1),(1,0),(1,2)}
CHAPTER 6 9
Graph terminology (cont.)
What is the number of edges in a
complete directed graph with N vertices?
N * (N-1)
2
O( N )
Graph terminology (cont.)
What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
2
O( N )
Graph terminology (cont.)
Weighted graph: a graph in which each edge
carries a value
Degree
The degree of a vertex is the number of
edges incident to that vertex
For directed graph,
– the in-degree of a vertex v is the number of
edges
that have v as the head
– the out-degree of a vertex v is the number of
edges
that have v as the tail
The degree of a vertex in a graph is
the number of edges incident on that
vertex.
CHAPTER 6 13
undirected graph
degree
3 0
0 2
1 2
3 1 23 3 3
3 4 5 6
3
G13 1 1 G2 1 1
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2
2 in: 1, out: 0
G3
CHAPTER 6 14
ADT for Graph
structure Graph is
objects: a nonempty set of vertices and a set of undirected edges, where
each
edge is a pair of vertices
functions: for all graph Graph, v, v1 and v2 Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
incident edge.
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
is removed
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
CHAPTER 6 15
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency Multilists
CHAPTER 6 16
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
CHAPTER 6 17
Examples for Adjacency Matrix
0 0 4
0
2 1 5
1 2
3 6
3 1
0 1 1 1 0 1 0
1 0 1 1 7
1 0 1
2 0 1 1 0 0 0 0 0
1 1 0 0 0 1 0
1 0
0 0 1 0 0 0
1 1 1 0
1 0 0 1 0 0 0 0
G2
G
1 0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
undirected: n2/2 0 0 0 0 0 0 1 0
directed: n2
CHAPTER 6 G4 18
Data Structures for Adjacency
Lists
Each row in adjacency matrix is represented as an adjacenc
#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use *
CHAPTER 6 19
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
An undirected graph with n vertices and e edges ==> n headCHAPTER
nodes6 and 2e list20no
Figure 6.10: Inverse adjacency list for G3
0
0 1 NULL
1 1 0 NULL
2 1 NULL
2
CHAPTER 6 21
Figure 6.11: Alternate node structure for adjacency lists (p.267)
tail head column link for head row link for tail
CHAPTER 6 22
Figure 6.12: Orthogonal representation for graph G3(p.268)
0 1 2
0 0 1 NULL NULL
0
2 NULL
0 1 0
1 0 1
0 0 0
1
2
CHAPTER 6 23
Figure 6.13:Alternate order adjacency list for G1 (p.268)
Order is of no significance.
headnodes vertax link
0 3 1 2 NULL
1 2 0 3 NULL
2 3 0 1 NULL
3 2 1 0 NULL
1 2
3 CHAPTER 6 24
Adjacency Multilists
An edge in an undirected graph is
represented by two nodes in adjacency
list representation.
Adjacency Multilists
–lists in which nodes may be shared among
several lists.
(an edge is shared by two different paths)
CHAPTER 6 25
Example for Adjacency Multlists
Lists: vertex 0: M1->M2->M3, vertex 1: M1->M4->M5
vertex 2: M2->M4->M6, vertex 3: M3->M5->M6
(1,0)
0 N 0 1 N2 N4 edge (0,1)
1 (2,0)
2 1 0 2 N3 N4 edge (0,2)
(3,0)
3 N 0 3 N5 edge (0,3)
(2,1)
0 2 1 2 N5 N6 edge (1,2)
(3,1)
N 1 3 N6 edge (1,3)
1 2 (3,2)
3 2 3 edge (2,3)
3
N six edges
CHAPTER 6 26
Adjacency Multilists
CHAPTER 6 27
Graph Operations
Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
CHAPTER 6 28
BFS: Level-by-level traversal
Given a starting vertex s
Visit all vertices at increasing distance from s
Visit all vertices at distance k from s
Then visit all vertices at distance k+1 from s
Then ….
1
The general BFS algorithm
Each vertex can be in one of three states:
Unmarked and not on queue
Marked and on queue
Marked and off queue
The algorithm moves vertices between these
states
2
Handling vertices
Unmarked and not on queue:
Not reached yet
Marked and on queue:
Known, but adjacent vertices not visited yet (possibly)
Marked and off queue:
Known, all adjacent vertices on queue or done with
3
Queue: A Resultant Array:
A
B E
G C D
4
Queue: A B E Resultant Array: A
A
B E
G C D
B E
G C D
6
Queue: A B E C G D F Resultant Array:
ABE
A
B E
G C D
Do same with E.
7
Queue: A B E C G D F Resultant Array:
ABEC
B E
G C D
Visit C.
Its neighbor F is already marked, so not queued.
8
Queue: A B E C G D F Resultant Array:
ABECG
B E
G C D
Visit G.
9
Queue: A B E C G D F Resultant Array:
ABECGD
B E
G C D
10
Queue: A B E C G D F Resultant Array:
ABE CGDF
B E
G C D
Visit F.
E, D, C marked, so not queued again.
11
Queue: A B E C G D F
A
B E
G C D
13
Depth-first search
1
DFS(graph g, vertex s)
Assume all vertices initially unmarked
1. mark s
2. visit s // e.g., print its data
3. for each edge <s, V>
4. if V is not marked
5. DFS(G, V)
2
Current vertex: A
A
B E
G C D
3
Current: B
A
B E
G C D
B E
G C D
5
Current: F
A
B E
G C D
B E
G C D
B E
G C D
B E
G C D
Adjacent B is A and G. F
Visit G.
No new vertices from here. Backtrack to A. E
already marked so no new vertices.
Current:
1
A
2
5
B E
3 6
G 7 C D
4
F
11
//Implementation of Bubble Sort
#include<stdio.h>
#include<conio.h>
#define max 100
void main()
{
int x[max],i,j,temp,n;
clrscr();
printf("\n Enter the size of an array :");
scanf("%d",&n);
printf("\n Enter the Elements in to an array:");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(x[j] > x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
} // end for innerloop
} // end for outer loop
for(i=0;i<n;i++)
printf("%d ",x[i]);
getch();
}
//Implementation of Selection Sort
#include<stdio.h>
#include<conio.h>
#define max 100
void main()
{
int x[max],i,j,temp,n,min;
clrscr();
printf("\n Enter the size of an array :");
scanf("%d",&n);
printf("\n Enter the Elements in to an array:\n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(x[min] > x[j])
min=j;
}
temp=x[i];
x[i]=x[min];
x[min]=temp;
} // end for outer loop