0% found this document useful (0 votes)
4 views40 pages

Module 4 - Done

Chapter 9 discusses the fundamentals of graphs, including definitions of undirected and directed graphs, complete graphs, subgraphs, and various representations such as adjacency matrices and lists. It also covers graph traversal methods like Depth First Search (DFS) and Breadth First Search (BFS), detailing their algorithms and complexities. Additionally, the chapter highlights the properties of connected graphs and the advantages and disadvantages of different graph representations.

Uploaded by

SHANIF
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 views40 pages

Module 4 - Done

Chapter 9 discusses the fundamentals of graphs, including definitions of undirected and directed graphs, complete graphs, subgraphs, and various representations such as adjacency matrices and lists. It also covers graph traversal methods like Depth First Search (DFS) and Breadth First Search (BFS), detailing their algorithms and complexities. Additionally, the chapter highlights the properties of connected graphs and the advantages and disadvantages of different graph representations.

Uploaded by

SHANIF
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/ 40

M4-Chapter 9-GRAPHS

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.

• The vertices are represented by circles, and the edges by lines.


• An edge with no orientation is an undirected edge.

Directed Graph

In a directed graph, each edge is represented by a directed pair 〈v1, v2〉. Here:

• v1 is the tail, and v2 is the head of the edge.


• 〈v2, v1〉 and 〈v1, v2〉 represent two different edges.
M4-Chapter 9-GRAPHS

• An edge with an orientation is a directed edge.


• If all the edges are directed, then the graph is a directed graph. A directed
graph is also called a digraph.

Complete Graph

• A complete graph is a simple undirected graph in which every pair of distinct


vertices is connected by a unique edge.
• Therefore, the complete digraph is a directed graph in which every pair of
distinct vertices is connected by a pair of unique edges (one in each direction).
• The complete graph on n vertices is denoted by Kn.
• Kn has n(n−1)/2 edges and is a regular graph of degree n−1.

Adjacent and Incident

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


o v0 and v1 are adjacent.
o The edge (v0, v1) is incident on vertices v0 and v1.
• If 〈v0, v1〉 is an edge in a directed graph:
o v0 is adjacent to v1, and v1 is adjacent from v0.
o The edge 〈v0, v1〉 is incident on v0 and v1.

Subgraph

A graph G1 = (V1, E1) is called a subgraph of a graph G = (V, E) if:

• V1(G1) is a subset of V(G).


• E1(G1) is a subset of E(G).

Each edge in G1 has the same end vertices as in G.


M4-Chapter 9-GRAPHS

G1 some of the subgraphs of G1

0 0 0 0

1 1
1
2
2

G3 some of the subgraphs of G3


M4-Chapter 9-GRAPHS

ADT for Graph


Structure Graph is

objects: an on empty set of vertices and a set of undirected edges,where


each edge is a pair of vertices

functions:for all graph E Graph, v, v1 and v2 E Vertices Graph

1. Create()::=return an empty graph


2. GraphInsertVertex(graph,v)::=return a graph with v inserted.v has no
Incident edge.

3. GraphInsertEdge(graph,v1,v2)::=return a graph with new edge


Between v1andv2

4. GraphDeleteVertex(graph,v)::=return a graph in which v and all edges


Incident to it are removed

5. GraphDeleteEdge(graph,v1,v2)::=returnagraphinwhichtheedge(v1,v2)
is removed

6. BooleanIsEmpty(graph)::=if(graph == emptygraph) return TRUE else


return FALSE
7. ListAdjacent(graph,v)::=return a list of all vertices that are adjacent to
v

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. Adjacent matrix Representation


and
2. Adjacency list representation
The choice of representation depends on the application and function to be
performed on the graph.

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.

Adjacency matrix for a weighted directed graph

It is similar to an adjacency matrix representation of a directed graph except that


instead of using the '1' for the existence of a path, here we have to use the weight
associated with the edge. The weights on the graph edges will be represented as the
entries of the adjacency matrix. We can understand it with the help of an example.
Consider the below graph and its adjacency matrix representation. In the
representation, we can see that the weight associated with the edges is represented
as the entries in the adjacency matrix.
M4-Chapter 9-GRAPHS

2.Adjacency List Representation:


In this representation, we store a graph as a linked structure. We store all the
vertices in a list and then for each vertex, we have a linked list of its adjacent
vertices.

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

• Adjacency lists save a lot of space.


• We can easily insert or delete edges as we use a linked list.
• This kind of representation is easy to follow and clearly shows the adjacent
nodes of a node.
M4-Chapter 9-GRAPHS

Disadvantages

• The adjacency list allows testing whether two vertices are adjacent to each
other, but it is slower to support this operation.

Adjacency MultiLists

• Adjacency Multi-lists are an edge, rather than vertex based, graph


representation. In the Multilist representation of graph structures;
these are two parts, a directory of Node information and a set of linked
list of edge information.
• There is one entry in the node directory for each node of the graph. The
directory entry fornode i points to a linked adjacency list for node i. each
record of the linked list area appears on two adjacency lists: one for the
node at each end of the represented edge.
Graph Traversal/Elementary Graph operation:
A graph traversal means visiting all the nodes of the graph. Two graph traversal
methods are commonly used. These are,
1. Depth First Search (DFS)
2. Breadth First Search(BFS)

1. DEPTH FIRST SEARCH:


In graphs, we do not have any start vertex or anyspecial vertex signaled out
to start traversal from. Therefore the traversal may start from any arbitrary
vertex.
We start with vertex v. An adjacent
vertex is selected and a depth first
M4-Chapter 9-GRAPHS

search is initiated from it. i.e.


V1,V2,….Vk are adjacent vertices to
vertex v. We may select any vertex
from this list. Say we select v1. Now all
the adjacent vertices to v1 are
identified and all of those are visited.
Next v2 is selected and all its adjacent
vertices visited and so on. This
process continues till all the vertices
are visited. Consider the following
graph.

Depth First Search:


1.Let us start with V1. Visit V1.
2. Its adjacent vertices are V2,V8, and V3. Let us pick on V2. Visit V2.
3. Its adjacent vertices are V1, V4, V5.
4. V1 is already visited. Let us pick on V4. Visit V4. Its adjacent vertices are V2,
V8.
5. V2 is already visited. Let us pick on V8. Visit V8. Its adjacent vertices are V4,
V5, V1, V6, V7.
6. V4 and V1 are already visited. Let us pick on V5. Visit V5. Its adjacent vertices
are V2, V8.
7. Both are already visited. Therefore we back track.
8. We have V6 and V7 unvisited in the list of V8. We may visit any. We visit V6.
Its adjacent are V8 and V3. Obviously the choice is V3. Visit V3.
9. Its adjacent vertices are V1, V7. We visit V7.
10. All the adjacent vertices of V7 are already visited, we backtrack and find that
M4-Chapter 9-GRAPHS

we have visited all the vertices. Therefore the sequence of traversal is


11. V1, V2, V4, V8, V5, V6, V3, V7.
We may implement the depth first search method by using a stack, pushing
all unvisited vertices adjacent to the one just visited and popping the stack to find
the next vertex to visit.

Function for Depth First search:


Algorithm dfs (vertex V)
{
visited [V] = true;
for each w adjacent to V if (!visited [w])
dfs(w);
}

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|).

2.BREADTH FIRST SEARCH


In DFS we pick on one of the adjacent vertices; visit all of the adjacent vertices
and back track to visit the unvisited adjacent vertices.
In BFS we first visit all the adjacent vertices of the start vertex and then visit
all the unvisited vertices adjacent to these and so on.
We start with V1. Its adjacent vertices
are V2, V8, V3. We visit all one by one.We
pick on one of these, say V2. The unvisited
adjacent vertices to V2 are V4, V5. We visit
both. We go back to the remaining visited
vertices of V1 and pick on one of those say
V3. The unvisited adjacent vertices are V6,
V7. There are no more unvisited adjacent
vertices of V8, V4, V5, V6 and V7.Thus the
sequence so generated is V1 V2 V8 V3
V4 V5 V6 V7.Here we need a queue instead
of a stack to implement it. We add
unvisited vertices adjacent to the one just
visited at the rear and read at front to find
the next vertex to visit.
M4-Chapter 9-GRAPHS

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

Complexity of BFS:The Operations of enqueing and dequeing take O(1)


time. So the total time devoted to queue operations is O(V).The total time
spent in scanning adjacency list is O(E). The total run time for BFS is O(V+E).

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

G1 and G2 are Connected graphs

G3

Graph G3 is not connected. It is one graph having two unconnected


components. Since there are unconnected components, it is an unconnected
graph
M4-Chapter 9-GRAPHS

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.

A digraph is called strongly connected if there is a directed path from


any vertex to any other vertex.

A weakly connected graph

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.

Let us make the above graph strongly connected as

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

iii) There is no cycle.


Such a structure is called spanning tree of graph. Take any vertex V as an
initial partial tree and add edges one by one so that each edge joins a new vertex
to the partial tree.

MINIMAL SPANNING TREE


Frequently we encounter weighted graphs and we need to build a sub-graph
that must include every vertex in the graph. To construct such a graph with least

weight or least cost we must not have cycles in it.

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

A connected undirected graph G is said to be biconnected if it remains


connected after removal of any one Vertex and the edges that are incident upon
that vertex.

A biconnected component of an undirected graph is a maximal biconnected


subgraph that is a biconnected sub graph not contained in any larger biconnected
sub graph.

Articulation point or cut point

A Vertex v is an articulation point or cut point for an undirected graph G if there


are distinct vertices w and x, distinct from v also such that v is in every path from
w to x.

An Un-Directed Graph
M4-Chapter 9-GRAPHS

Bi-Connected Components of Above Graph

Articulation Point:After deleting vertex B and its incident edges the given graph
is divided into two non empty components.

Graph Obtained after Deleting B from the Graph

After deleting vertex E and its incident edges the resulting non empty components
M4-Chapter 9-GRAPHS

are shown below.

Graph Obtained after Deleting E from the Graph

After deleting vertex F and its incident edges the resulting non empty components

are shown below.

Graph Obtained after Deleting F from the Graph

From the above graphs we can say that B, E and F are the articulation points
of the given graph.

Construction of Bi-Connected Graph:::

i) Check whether the given graph is bi-connected or not

ii) If the given graph is not bi-connected then identify all the Articulation
points.
M4-Chapter 9-GRAPHS

iii) If Articulation points exist, determine a set of edges whose inclusion

makes the graph bi-connected

An Un-Directed Graph

• Given graph is not a bi-connected graph.

• The Articulation Points are 2,3,5

• To Transform the given graph into bi-connected graph, the new


edges are included corresponding to the Articulation point.

• Edges corresponding to the Articulation point 3-(4, 10) (10, 9)

• Edges corresponding to the Articulation point 2-(1, 5) (3, 8)

• Edges corresponding to the Articulation point 5-(6, 7).

You might also like