Module 4 - Done
Module 4 - Done
M4 – Chapter 9
Graphs
GRAPHS
A graph G consists of a set of V or vertices (nodes) and a set of edges (arcs). We
write G = (V, E).V is a finite non empty set of vertices.E is a set of pairs of
vertices. These pairs are called edges.
An edge e=(v,w), is a pair of vertices v and w, and is said to be incident with v and
w.
Undirected Graph
In an undirected graph, the pairs of vertices representing any edge are unordered. Thus,
the pairs (v1,v2) and (v2,v1) represent the same edge.
Directed Graph
In a directed graph, each edge is represented by a directed pair 〈v1, v2〉. Here:
Complete Graph
Subgraph
0 0 0 0
1 1
1
2
2
5. GraphDeleteEdge(graph,v1,v2)::=returnagraphinwhichtheedge(v1,v2)
is removed
Graph Representation
Graph is a mathematical structure and finds its application in many areas
of interest in which problems need to be solved using computers. Thus this
mathematical structure must be represented as some kind of data structures.
Two such representations are commonly used. These are,
M4-Chapter 9-GRAPHS
1.Adjacency Matrix:
The adjacency matrix A for graph G = (V,E) with n vertices, is an nxn matrix of
d bits, such that,
Aij = 1 if there is an edge from vi to vj
and Aij = 0 if there is no such edge.
You may observe that the adjacency matrix for an undirected graph is
symmetric, as the lower and upper triangles are same. Also all the diagonal
elements are zero.
M4-Chapter 9-GRAPHS
The total number of 1’s account for the number of edges in the digraph. The
number of 1’s in each row tells the out degree of the corresponding vertex.
The adjacency list representation needs a list of all of its nodes. i.e.
V1
V2
V3
M4-Chapter 9-GRAPHS
V4
V5 V3
V6
And for each node a linked list of its adjacent node. Therefore we shall have
Note that adjacent vertices may appear in the adjacency list in arbitrary order. Also
an arrow from v2 to v3 in the list linked to v1 does not mean that v2 and v3 are
adjacent
M4-Chapter 9-GRAPHS
Now, consider the weighted directed graph, and let's see the adjacency list
representation of that graph.
In the case of a weighted directed graph, each node contains an extra field that is
called the weight of the node.
In an adjacency list, it is easy to add a vertex. Because of using the linked list, it also
saves space.
Advantages
Disadvantages
• The adjacency list allows testing whether two vertices are adjacent to each
other, but it is slower to support this operation.
Adjacency MultiLists
Complexity of DFS: The procedure DFS is called once for each vertex of G.
We can calculate the complexity of DFS by adding up the time taken by each
of these calls to DFS. There are two parts to each call, the marking process and
the for loop. The marking process takes a constant amount of time say C1. Each
time through the loop, the for loop takes a constant amount of time to test the
condition say C2. And a constant amount of time say c3 as an upper bound for
the time to execute the body of the loop.
For each vertex, the loop will be calculated once for each entry on the vertex’s
adjacency list. The total time to execute the loop for a single vertex V is bounded
M4-Chapter 9-GRAPHS
by C1+(C2+C3).
Time complexity = O(|V|+|E|).
V1
V1 V1
V2 V8 V3
V2 V3
V8
V4 V5
V1
Function for bfs
bfs (vertex v) V2 V8 V3
{
vertex w; V4 V5 V6 V7
queue q;
visited [v] = true;
initialise (q);
addqueue (q,v)
while (! Emptyqueue(q))
{
deletequeue (q,v);
for all vertices w adjacent to v
if (!visited [w])
{
addqueue (q,w);
visited[w] = true;
}
M4-Chapter 9-GRAPHS
}
}
The sequence generated is V1 V2 V8 V3 V4 V5 V6 V7.
bfs(v1)
• visit v1
• add v1 to queue
• delete queue now v is v1
• adjacent vertices of V1 are v2, v8, v3
• unvisited vertices are visited and added to queue. V2 v8 v3 are visited
and added to queue delete queue. Now v is v2
• adjacent vertices of V2 are v1, v4, v5
• v1 is already visited . unvisited vertices v4, v5 are visited and
added to queue delete queue. Now V is V8
• adjacent vertices of V8 are V4, V5,V1,V6,V7
• V4,V5,V1 are already visited . unvisited vertices V6, V7 are visited and added
to queue
• delete queue. Now V is V3
• adjacent vertices of V3 are V1,V6,V7
• V1,V6,V7 are already visited . There are no unvisited
vertices of V3 Delete queue Now V is V4
• Adjacent Vertices of V4 are V2 V8 both are
already visited And so on….
M4-Chapter 9-GRAPHS
DFS Vs BFS
DFS BFS
Method The DFS algorithm explores BFS is exactly the opposite of
each possible path to its DFS. In this method, each node
conclusion before another on the same level is checked
path is tried. before the search
proceeds to the next level.
Data structure used Stack (LIFO list) QUEUE(FIFO list)
Type of edges Back edges Cross edges
Complexity O(|V|+|E|) O(|V|+|E|)
Applications Spanning forest, connected Spanning forest, connected
components path and cycles, components path and cycles,
Biconnected components, shortest path, to produce a
articulation point reverse topological ordering
of nodes.
M4-Chapter 9-GRAPHS
3.Connected Graph:
In an undirected graph G, two vertices are said to be connected iff there is a
path from u to v ( since G is an undirected graph this means there must also be
a path from v to u). An undirected graph is said to be connected iff for every
pair of distinct vertices u and v in V(G), there is a path from u to v in G.
a) G1 b)G2
G3
STRONGLY CONNECTED
A tree is a connected acyclic graph (which has no cycles). A directed graph is
said to be strongly connected iff for every pair of distinct vertices u and v in
V(G), there is a directed path from u to v and also from v to u.
There does not exist a directed path from vertex 1 to vertex 4 also from vertex 5
to other vertices and so on. Therefore it is a weakly connected graph.
Strongly
M4-Chapter 9-GRAPHS
ConnectedGraph
4.SPANNING TREE
A spanning tree for a connected undirected graph G=(V,E) is a sub group of G
that is an undirected tree and contains all the vertices of g. A spanning tree of
a graph should include all the vertices and a subset of edges.
Consider the following graph and some of the tree structures for the graph.
M4-Chapter 9-GRAPHS
You may notice that all the spanning trees differ from each other significantly,
however for each structure
i) The vertex set is same as that of graph G
ii) The edge set is a subset of G(E) and
M4-Chapter 9-GRAPHS
Consider the above graph. The MST for this graph could be
building a least cost communication network.
We begin by first selecting an edge with least cost, it can
between any two vertices of graph G. Subsequently from the
set of remaining edges, we can select another least cost edge
and so on. Each time an edge is picked, we determine
whether or not the inclusion of this edge into the spanning
tree being constructed creates a cycle. If it does this edge is
discarded. If no cycle is created, this edge is included in the spanning tree being
constructed. The minimum cost is BA.
M4-Chapter 9-GRAPHS
5.BICONNECTED COMPONENTS
An Un-Directed Graph
M4-Chapter 9-GRAPHS
Articulation Point:After deleting vertex B and its incident edges the given graph
is divided into two non empty components.
After deleting vertex E and its incident edges the resulting non empty components
M4-Chapter 9-GRAPHS
After deleting vertex F and its incident edges the resulting non empty components
From the above graphs we can say that B, E and F are the articulation points
of the given graph.
ii) If the given graph is not bi-connected then identify all the Articulation
points.
M4-Chapter 9-GRAPHS
An Un-Directed Graph