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

Module 5 - Chapter1_Graphs

Module 5 covers graphs, including definitions, terminologies, and representations such as adjacency matrices and lists, along with elementary graph operations. It also discusses hashing techniques and presents case studies for designing a music playlist system and managing restaurant table reservations. The module concludes with an overview of graph traversal methods like Depth First Search (DFS) and Breadth First Search (BFS).

Uploaded by

mohsiinbashir
Copyright
© © All Rights Reserved
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)
4 views

Module 5 - Chapter1_Graphs

Module 5 covers graphs, including definitions, terminologies, and representations such as adjacency matrices and lists, along with elementary graph operations. It also discusses hashing techniques and presents case studies for designing a music playlist system and managing restaurant table reservations. The module concludes with an overview of graph traversal methods like Depth First Search (DFS) and Breadth First Search (BFS).

Uploaded by

mohsiinbashir
Copyright
© © All Rights Reserved
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/ 43

Data Structures and

Applications – Module 5

Department of Computer Science & Engineering

www.cambridge.edu.in
Module 5
Graphs: Definitions, Terminologies, Matrix and Adjacency List Representation of Graphs, Elementary Graph operations.
Hashing: Hash Table organizations, Hashing Functions, Static and Dynamic Hashing
Summarization of all modules.
Case Studies:
a. Design a music playlist system: The functionality of a playlist needs to be implemented, i.e., adding a song to the playlist,
playing the next song, playing the previous song, switching to a song, display of songs based on its genre type etc.
b. Efficiently manage table reservations for a restaurant: Managing table reservations for a restaurant efficiently involves
handling various aspects such as availability of tables, booking requests, and ensuring a smooth flow of operations.
5.1 Definition
Graph is a non-linear data structure, It contains a set of points known as nodes (or vertices) and set of links known
as edges(or Arcs) which connects the vertices.
 A graph G consists of two sets

– a finite, nonempty set of vertices V(G)


– a finite, set of pair of vertices called edges E(G)
– G(V,E) represents a graph
 An undirected graph is a graph in which the pair of vertices representing any edge is unordered, (i.e) – (u,v) and
(v,u) represent the same edge, (u, v) = (v,u).
 A directed graph is a graph in which each edge is represented by a directed pair of vertices <u,v>; u is the tail
and v is the head of the edge, (i.e) – (u,v) and (v,u) represent two different edges, <u, v> != <v,u>

Digraph- Directed graph tail:u


Graph – Undirected graph
<u, v> : head:v
Examples for Graph
0 0 0

1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graphs 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>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
Trees are special case of graph that is a tree is a acyclic graph.
5.2 Graph Data Structure - Terminologies
• Vertex − Each node of the graph is represented as a vertex.
• Edge − Edge represents a path between two vertices or a line between two vertices.
• Adjacency − Two node or vertices are adjacent if they are connected to each other
through an edge.
• Path − Path represents a sequence of edges between two vertices.
• Cycle: A path that starts and ends on the same vertex
• Directed Graph: A graph that entail edges with ordered pair of vertices and has direction
indicated with an arrow.
• Undirected Graph: A graph that entail edges with ordered pair of vertices, however it
does not have direction define.
• Weighted Graph: A graph in which each edge carries a value.
5.2 Graph Data Structure - Terminologies
• Complete Graph: A graph in which every vertex is directly connected to every other vertex.
• A complete graph is a graph that has the maximum number of edges
 for undirected graph with n vertices, the maximum number of edges is n(n-1)/2
 for directed graph with n vertices, the maximum number of edges is n(n-1)
 example: G1 is a complete graph

• If (v0, v1) is an edge in an undirected graph,


• v0 and v1 are adjacent
• The edge (v0, v1) is incident on vertices v0 and v1
• If <v0, v1> is an edge in a directed graph
• v0 is adjacent to v1, and v1 is adjacent from v0
• The edge <v0, v1> is incident to v0 and v1
• The degree of a vertex is the number of edges incident to that vertex. In an undirected graph degree of a vertex V is
number of edges incident to vertex V.
• 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
5.2 Graph Data Structure - Terminologies
3 0
degree 0 2
1 2
undirected graph
3 1 2 3 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
5.2 Graph Data Structure - Terminologies

0 2 1 3

1 2
self edge multigraph:
(a) (b) multiple occurrences
We impose the following restrictions on graphs - A of the same edge
graph will not have:
1) self edges
2) multiple Occurences of the same edge
5.2 Graph Data Structure - Terminologies

 Subgraph: If graph G=(V, E) Then Graph G'=(V',E') is a subgraph of G if V' ⊆ V and E' ⊆
E
 A path from vertex u to vertex v in a graph G, is a sequence of vertices, u, i1, i2, ..., ik, v,
such that (u, i1), (i1, i2), ..., (ik, v) are edges in E(G).
 The length of a path is the number of edges on it.
5.2 Graph Data Structure - Terminologies
subgraphs of G1 and G3
0 0 0 1 2 0

1 2 1 2 3 1 2
3
3
G1 (i) (ii) (iii) (iv)
(a) Some of the subgraph of G1

0 0 0 0
0
1 1 1
1
2 2
(i) (ii) (iii) (iv)
2 (b) Some of the subgraph of G3

G3
5.2 Graph Data Structure - Terminologies

connected

0 0

1 2 1 2
3
3 4 5 6
G1 tree (acyclic graph)
G2
• An undirected graph is connected iff, for every pair of distinct vertices u, v in V(G) there is a path from u to v in G.
• A connected component, H of an undirected graph (G) is a maximal connected subgraph.
• Maximal subgraph: A maximum subgraph is one that cannot be expanded farther from the original graph by
adding more vertices or edges without violating certain properties.
• A tree is a connected, acyclic (no cycles) graph.
A graph with two connected components

H1 0 H2 4

2 1 5

3 6

G4 (not connected) 7

H1 and H2 are connected components of G4 (maximal connected subgraph)


Figure: Strongly connected components of G3
strongly connected component
not strongly connected (maximal strongly connected subgraph)
A directed graph G is
strongly connected iff for 0
every pair of distinct 0 2
vertices u and v in V(G),
there is a directed path
from u to v and also from v 1
to u.
A strongly connected
component is a maximal 1
subgraph that is strongly 2
connected. G3 is not strongly connected, as there is no path from vertex 2 to 1.
G3 G3 has two strongly connected components.
ADT for Graph
ADT 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
5.3 Graph Representations
Following two are the most commonly used representations of graph.

 Adjacency Matrix
 Adjacency Lists
Adjacency Matrix and Adjacency List
 Let G=(V,E) be a graph with n vertices, n>=1.
 The adjacency matrix of G is a two-dimensional n by n array, say a, with the property
that a[i][j]=1 iff the edge(i,j) is in E(G). a[i][j]=0 if there is no such edge in G.
 The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a
digraph need not be symmetric. n 1
 
For an undirected graph, the degree of any vertex i is its row sum: a[i ][ j ]
j 0

 For a directed graph the row sum is the out-degree, and the column sum is the in-degree.
 In adjacency list representation, the n rows of adjacency matrix are represented as n
chains. There is one chain for each vertex in G.
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 1 0 0 0 1
   0 0 1 0 0 0 0
1 1 1 0
1 0 0 1 0 0 0 0
G2  
G1
0 1 1 0 0 0 0 0
0 1 2 3 0 0 0 0 0 1 0 0
0 0 1 1 1  
0 0 0 0 1 0 1 0
1 1 0 1 1 0
symmetric 0 0 0 0 1 0 1
2 1 1 0 1  
3 1 1 1 0 0 0 0 0 0 0 1 0

G4
0 0 4
Examples for 2 1 5
Adjacency List 1 2 3 6
3 7
0 1 2 3 NULL
0 1 2 NULL

1 0 2 3 NULL
1 0 3 NULL

2 0 1 3 NULL
2 0 3 NULL

3 0 1 2 NULL
3 1 2 NULL

G1 0 4 5 NULL

5 4 6 NULL

0 1 NULL

6 5 7 NULL

1 0 2 NULL 1
7 6 NULL

2 NULL

G2 G3
2
Examples for Adjacency Matrix and Adjacency List

The adjacency list representation of the above graph


The adjacency matrix for the above
example graph is:
Elementary Graph Operations
 Traversal
Given G=(V,E) and vertex v, find all wv, such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
 Connected Components
 Spanning Trees
 Biconnected Components
Depth First Search Traversal
• Depth First Search algorithm(DFS) traverses a graph in a depth ward motion and uses a stack to remember to
get the next vertex to start a search when a dead end occurs in any iteration.
• Algorithm
1. Visit adjacent unvisited vertex. Mark it visited. Display it. Push it in a stack.
2. If no adjacent vertex found, pop up a vertex from stack. (It will pop up all the vertices from the stack
which do not have adjacent vertices.)
void dfs(int s)
3. Repeat step 1 and 2 until stack is empty.
{
visited[s]=1;
printf(“%d\t”,s);
for(i=0;i<n;i++)
{
if(a[s][i]==1 && visited[i]==0)
dfs(i);
}
}
2. Mark S as visited and put it onto the stack.
DFS Example • Explore any unvisited adjacent node from S.
• We have three nodes and we can pick any of
1. Initialize the stack them.
• For this example, we shall take the node
in alphabetical order.

Output - S
4. Visit D and mark it visited and put onto the stack.
DFS Example • Here we have B and C nodes which are adjacent
to D and both are unvisited.
• But we shall again choose in alphabetical order.
3. Mark A as visited and put it onto the stack.
• Explore any unvisited adjacent node from A.
• Both S and D are adjacent to A but we are
concerned for unvisited nodes only.

Output – S-A-D

Output – S-A
6. We check stack top for return to previous node
DFS Example and check if it has any unvisited nodes.
• Here, we find D to be on the top of stack.
5. We choose B, mark it visited and put onto stack.
• Here B does not have any unvisited adjacent
node. So we pop B from the stack.

Output – S-A-D-B
Output – S-A-D-B
DFS Example

7. Only unvisited adjacent node is from D is C now.


So we visit C, mark it visited and put it onto the stack.

Output – S-A-D-B-C

8. As C does not have any unvisited adjacent node so we


keep popping the stack until we find a node which has
unvisited adjacent node.
• In this case, there's none and we keep popping until
stack is empty.
Breadth First Search Traversal
• Breadth First Search algorithm(BFS) traverses a graph in a breadth wards motion and uses a queue to remember to get the next
vertex to start a search when a dead end occurs in any iteration.
• Algorithm (We start from visiting S (starting node), and mark it visited)
1. Visit adjacent unvisited vertex. Mark it visited. for(i=0;i<n;i++)
Display it. Insert it in a queue. {
2. If no adjacent vertex found, remove the first vertex from queue. if(a[s][i]==1 && visited[i]==0)
3. Repeat step 1 and 2 until queue is empty. {
void bfs(int s) q[++rear]=i;
{ visited[i]=1;
int q[n], front=0,rear=-1; }
visited[s]=1; }
while(1) if(front>rear)
{ return;
printf(“%d\t”,s); s=q[front++];
}
}
BFS Example 2. We start from visiting S (starting node), and mark it
visited.

1. Initialize the queue.

Output – S
BFS Example 4. Next unvisited adjacent node from S is B.
We mark it visited and enqueue it.

3. We then see unvisited adjacent node from S.


In this example, we have three nodes but
alphabetically we choose A mark it visited and
enqueue it.

Output – S - A Output – S – A – B
BFS Example 6. Now S is left with no unvisited adjacent nodes.
So we dequeue and find A.
5. Next unvisited adjacent node from S is C.
We mark it visited and enqueue it.

Output – S – A – B – C
BFS Example

7. From A we have D as unvisited adjacent node.


We mark it visited and enqueue it.

Output – S – A – B – C – D

8. At this stage we are left with no unmarked (unvisited) nodes.


But as per algorithm we keep on dequeuing in order to get all
unvisited nodes.
• When the queue gets emptied the program is over.
Traversals – DFS, BFS Example

Graph G and its adjacency lists – Find the order of vertices in dfs, and bfs.
depth first search: v0, v1, v3, v7, v4, v5, v2, v6

breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
Connected Components
void isconnected(int s)
{
dfs(s);
 An graph is connected iff,
for(i=0;i<n;i++)
for every pair of distinct {
vertices u, v in V(G) there is if(visited[i]==0)
a path from u to v in G. {
printf(“not connected”);
 Connectivity of the graph
return;
can be detected by calling }
DFS/BFS of every vertex }
repeatedly for the set of n printf(“connected”);
vertices. }
Spanning Trees
 When graph G is connected, a depth first or breadth first search starting at any vertex will
visit all vertices in G
 A spanning trees are acyclic subgraphs of a given graph such that it covers all the vertices
of the graph and contains n-1 edges where n is the number of vertices.
 A spanning tree is a minimal subgraph, G’, of G such that V(G’)=V(G) and G’ is connected.
 Any connected graph with n vertices must have at least n-1 edges.
 E(G): T (tree edges) + N (nontree edges)
where T: set of edges used or traversed during search
N: set of remaining edges
The edges in T form a tree that includes all vertices of G.(i.e) the spanning tree.
Examples of Spanning Tree

0 0 0 0

1 2 1 2 1 2 1 2
3 3 3 3

G1 Possible spanning trees


Spanning Trees
 Either dfs or bfs can be used to create a spanning tree
– When dfs is used, the resulting spanning tree is known as a depth first
spanning tree.
– When bfs is used, the resulting spanning tree is known as a breadth first
spanning tree
 While adding a nontree edge into any spanning tree, this will
create a cycle
DFS VS BFS Spanning Tree

0 0 0

1 2 1 2 1 2

3 4 5 6 3 4 5 6 3 4 5 6
nontree edge
7 7 cycle 7
DFS Spanning BFS Spanning
Biconnected Components
A biconnected graph is a connected graph that has no articulation points.
An articulation point is a vertex v of G such that the deletion of v, together with all edges
incident on v, produces a graph G’, that has atleast two connected components.

This graph is not a biconnected graph as it has


articulation points 0 biconnected graph

1 2

3 4 5 6

7
0 8 9 Biconnected
1 7 Components
connected graph

2 3 5
Removal of 2 - one connected
Removal of 3 - two connected graph, , so 2 is not an articulation
components, so 3 is an articulation 4 6
point
point
0 8 9 0 8 9

7 1 7
1

2 3 5 2 3 5

4 6 4 6
Biconnected Components
Biconnected
component: a maximal 0 8 9
biconnected subgraph
H of G. 8 9
0
(Maximal means, G 7 7
1
contains no other
subgraph that is both
1 7
biconnected and 1 7
properly contains H) –
no edge can be in two 2 3 5
2 3 3 5 5
or more biconnected
components of G
4 6
4 6
Connected Graph, G Biconnected components of G
Biconnected Components
0
3
4 0 9 8 9 8 1 5
nontree 4 5
1 7 7 edge
3 (back edge) nontree
0 5 2 2 6 6 edge
(back edge)
2 2 3 5 3
dfn
depth 1 7 7
first 8 9
4 number 6
4 0 9 8
1 6
(a) depth first spanning tree (b)

If u is an ancestor of v then dfn(u) < dfn(v).


Biconnected vertex
0
dfn
4
low
4 (4,n,n)
child
null
low_child
null
low:dfn
null:4
1 3 0 (3,4,0) 0 4 43 
Components 2 2 0 (2,0,n) 1 0 0<2
3 0 0 (0,0,n) 4,5 0,5 Root (2 children) 
Vertex 0 1 2 3 4 5 6 7 8 9
4 1 0 (1,0,n) 2 0 0<1
dfn 4 3 2 0 1 5 6 7 9 8 5 5 5 (5,5,n) 6 5 55 
6 6 5 (6,5,n) 7 5 5<6
low 4 0 0 0 0 5 5 5 9 8 7 7 5 (7,8,5) 8,9 9,8 9,8  7 
8 9 9 (9,n,n) null null null, 9
dfn and low values for dfs spanning tree
9 8 8 (8,n,n) null null null, 8
with root =3 3 0
low(u)=min{dfn(u), min{low(w)|w is a child of u},
1 5 min{dfn(w)|(u,w) is a back edge}
4 5 u: articulation point if, low(child)  dfn(u)
• The root of a depth first spanning tree is an articulation point iff
it has at least two children.
2 2 6 6 • Any other vertex u is an articulation point iff it has at least one
child w such that we cannot reach an ancestor of u using a path
1 3 7 7 that consists of (1) only w (2) descendants of w (3) single back
edge.
8 9
4 0 9 8
Biconnected vertex
0
dfn
0
low
0 (0,1,n)
child
1
low_child
1
low:dfn
Root :1 child
Components 1 1 1 (1,1,n) 2 1 11 
2 2 1 (2,1,n) 4 1 1<2
dfn and low values for dfs spanning tree 3 4 1 (4,5,1) 5 5 54
with root =0
4 3 1 (3,1,n) 3 1 1<3
5 5 5 (5,5,n) 6 5 55 
6 6 5 (6,5,n) 7 5 5<6
7 7 5 (7,8,5) 8,9 8 87
8 8 8 (8,n,n) null null null, 8
9 9 9 (9,n,n) null null null, 9
low(u)=min{dfn(u), min{low(w)|w is a child of u},
min{dfn(w)|(u,w) is a back edge}
u: articulation point if, low(child)  dfn(u)
• The root of a depth first spanning tree is an articulation point iff
it has at least two children.
• Any other vertex u is an articulation point iff it has at least one
child w such that we cannot reach an ancestor of u using a path
that consists of (1) only w (2) descendants of w (3) single back
edge.
void dfnlow(int u, int adj[MAX][MAX], int V)
{
int children = 0,v;
visited[u] = 1;
dfn[u] = low[u] = ++time;
for (v = 0; v < V; v++)
{
if (adj[u][v])
{
// If there's an edge between u and v
if (!visited[v])
{
children++;
parent[v] = u;
dfnlow(v, adj, V);
// Check if the subtree rooted at v has a connection back to one of the ancestors of u
low[u] = (low[u] < low[v]) ? low[u] : low[v];
// u is an articulation point in the following cases:
// Case 1: u is the root of DFS tree and has two or more children.
if (parent[u] == -1 && children > 1)
articulation[u] = 1;
// Case 2: u is not the root and low value of one of its children is more than discovery value of u.
if (parent[u] != -1 && low[v] >= dfn[u])
articulation[u] = 1;
}
else if (v != parent[u])
{
// Update low value of u for parent function calls.
low[u] = (low[u] < dfn[v]) ? low[u] : dfn[v];
}
}
}
}

You might also like