Module 5 - Chapter1_Graphs
Module 5 - Chapter1_Graphs
Applications – Module 5
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
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
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
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
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
Output – S-A-D-B-C
Output – S
BFS Example 4. Next unvisited adjacent node from S is B.
We 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
Output – S – A – B – C – D
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
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.
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)