anandhavalli.files.wordpress.com  · web viewthe algorithm eventually halts after backing up to...

35
Chapter 9 E2 G (V, E) = V1 V2 V3 V4 E1 E4 E3 E5 Graphs ADT GRAPHS ADT 9.1. INTRODUCTION Graph is the one more important non-linear DS that doesn’t have linear or hierarchical relationship between its elements. Definition A graph G = (V, E) consists of nonempty set of vertices ‘V’ and set of types ‘E’. Vertex is nothing but a node which holds the data elements and it is represented by a circle. Edge is an arc between any two vertices or pair of vertices. If there is an edge from vertex V 1 to V 2 then it will be represented by (V 1 ,V 2 ). For example 9.2. TYPES OF GRAPH ADT Based on the direction or flow of an edge between a pair of vertices a graph can be divided as, 1. Undirected graph 2. Directed graph 1. Undirected graph It is a graph in which all the edges are represented by an unidirectional head. If two vertices v1,v2 are connected Data Structures Using C 223 Where V(G) = {V1,V2,V3,V4} E(G) = {(V1,V2), (V2,V4), (V4,V3), (V1,V3), (V3,V2)}

Upload: others

Post on 14-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

Chapter

9

E2G (V, E) =

V1 V2

V3 V4

E1

E4

E3

E5

d

e

b

c

a

Graphs ADT

GRAPHS ADT

9.1. INTRODUCTION Graph is the one more important non-linear DS that doesn’t have linear or

hierarchical relationship between its elements.Definition

A graph G = (V, E) consists of nonempty set of vertices ‘V’ and set of types ‘E’. Vertex is nothing but a node which holds the data elements and it is represented by a circle. Edge is an arc between any two vertices or pair of vertices. If there is an edge from vertex V1 to V2 then it will be represented by (V1,V2).For example

9.2. TYPES OF GRAPH ADT Based on the direction or flow of an edge between a pair of vertices a graph can be

divided as, 1. Undirected graph 2. Directed graph1. Undirected graph

It is a graph in which all the edges are represented by an unidirectional head. If two vertices v1,v2 are connected by an unidirectional arc then the flow from v1 to v2 and from v2 to v1 are same. (i.e.) (v1,v2) = (v2,v1)Example

Data Structures Using C 223

Where V(G) = {V1,V2,V3,V4}E(G) = {(V1,V2), (V2,V4), (V4,V3), (V1,V3), (V3,V2)} (or) E(G) = {E1,E2,E3,E4,E5}

Where (a,b) = (b,a)

Page 2: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

V1

V2 V3

V2V1V1 V2

V1V1

a b

c

Path = {(a,b), (b,c), (c,a)}

CD

BA

Indegree of D is 3. Outdegree of A is 2.

B

D C

Degree of A is ‘2’

A

Graphs ADT

2. Directed graph It is also called as “diagraph”. It is a graph, in which all the edge have direction

head from one vertex to another.If a vertex v1 is connected to v2 through a directional edge then the flow from v1 to

v2 alone possible. (i.e.) (v1,v2) ≠ (v2,v1).Example

Properties of Graphs ADT A graph can have a self loop (i.e.) an edge whose start and end vertices are same

(v1,v1).

A graph can contain parallel edges (i.e.) more than one edge between the same pair

of vertices.

A vertex v1 is adjacent to a vertex v2 only if there is an edge from v1 to v2. A graph which doesn’t have a self loop or parallel edges is called as simple graph. A graph which has self loop or parallel edge or both is referred as multi graph. If

there is a path containing one or more edges which starts from a vertex and terminates into the same vertex then the path is called a cycle.

A graph that has cycle path is referred as cyclic graph. If it doesn’t contains a cyclic path then called as acyclic graph.

Degree of a vertex is a number of edges incident or connected to that vertex. In digraph, the degree of a vertex is classified as indegree and outdegree.

Indegree - Number of edges pointing or coming inside to that vertex. Outdegree - Number of edges going outside from that vertex.

Data Structures Using C 224

Page 3: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

6G (V, E, W) =

V1 V2

V3 V4

5

10

8

1

4 3

2

Complete.

V4 V3

V2V1

Not-Complete. Because there is no edge from V2 to V4.

Graphs ADT

Source vertex is a vertex has indegree 0 & Sink vertex is a vertex has outdegree 0. Removal of node or vertex from graph, disconnects or breaks the graph into two or

more graphs then the vertex is called Articulation point. A graph with no articulation point is called as Bi-connected graph. Removal of any edge from the graph disconnects the graph into two graphs then that

edge is called Bridge. N is a number of vertices in a graph then undirected graph have N (N-1) / 2

maximum edges and in directed graph only N (N-1) maximum edges.

9.3. BASIC TERMINOLOGIES

Weighted graphA graph is said to be weighted only if all the edges have a weight value (numerical

value). It may be either directional or unidirectional graph. It is denoted by G (V, E, W). Example

Complete graphA graph is complete only if each vertex is adjacent to every other vertex in graph

(i.e.) a graph should have maximum edges on it.Example

Planar Graph

Data Structures Using C 225

2

Page 4: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

1

2 3

4 5

6

Non Planar

Planar

1 2

3 4

1

3 4

2

Connected Graph

32

41

Not Connected Graph

2

1

3

4

5

1

3

2

Graphs ADT

A graph is planar if it can be drawn in a plane without any intersection between two edges.

Connected GraphA graph is said to be connected if there is a path in ‘G’ from any node to any other

node (i.e.) for every pair of distinct vertices in G, there is a path.

Strongly Connected Graph A directed graph is said to be strongly connected if and only if every pair of distinct

vertices in ‘G’, there will be a path. (i.e.) if there is a path from ‘u’ to ‘v’ then also have a path from ‘v’ to ‘u’.

Weakly Connected GraphA connected graph is said to be weakly connected if and only if there is a path from

‘u’ to ‘v’, but not from ‘v’ to ‘u’ vertex.

9.4. REPRESENTATION OF GRAPH

Data Structures Using C 226

Strongly Connected Graph

Path from 1 to 2 exist but 2 to 1 not exist.

Page 5: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A B

D C

3

4

2

6 5

A B

D C

A B

D C

Graphs ADT

There are two standard ways to represent a graph in a computer, which is applicable to both directed and undirected graph.

1. Adjacency Matrix.2. Adjacency List.

1. Adjacency Matrix It is performed when the graph is dense (i.e.) |E| is close to |V|2 (or) quickly we need

to tell if there is an edge between two given vertices. It consist of |V|*|V| matrix A= (aij) such that

aij = ‘1’ if there is an edge from i to j where (i,j) belongs to E. ‘0’ otherwise

It is also called bit or Boolean matrix. In the directed graph, the sum of each row gives the outdegree of vertex in the row and the sum of each column is the indegree of a vertex represented by that column.

The space needed to represent a graph |V|2. For undirected graph, the matrix value represents the number of edges between any

two vertices. For a weighted graph the value of matrix aij is,

aij= wij if there is an edge from i to j where (i,j) belongs to E 0 otherwise

Example: Mention the adjacency matrix for the following graph

Example: Draw the graph for the following adjacency matrix.

A(G) =

The graph has four vertices name as V1, V2, V3, and V4. It is an undirected graph because the values are symmetric. So the graph becomes,

Data Structures Using C 227

A B C DA 0 3 0 6B 0 0 4 5C 0 0 0 0D 0 0 2 0

A B C DA 0 1 0 1B 0 0 1 0C 0 0 0 0D 0 0 1 0

A B C DA 0 1 1 1B 0 0 1 0C 0 1 0 0D 0 0 1 0

0 0 1 10 0 1 01 1 0 11 0 1 0

Page 6: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

V1

V3 V4

V2

(OR)

V1

V4 V3

V2

1

4 3

2 1

2

3

4

2 4

3

2

3

3

6 4

2

5

A B

D C

A

B

C

D

B 3

C 4 D 5

C 2

D 6

NULL

Graphs ADT

2. Adjacency List Representation It is usually performed to represent a graph, because it provides a compact way to

represent a square graph. The space needed to represent a graph is much less than |V|2. This consist of an array Adj of |V| lists, one for each vertex in V. For each vertex u

belongs to v, the adjacency list Adj[u] contains all vertices v such that there is an edge (u, v). The vertices in adjacency list are typically stored in arbitrary order and it is formed like a linked list.

For non-weighted graph the node structure is,

For weighted graph,

Example

9.5. GRAPH TRAVERSAL

Data Structures Using C 228

Data Next Pointer

Data Weight Next Pointer

Page 7: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

1 2

3 4

5

6 8

7

G W W W

W W W W

Graphs ADT

Traversing the graph is nothing but visiting each node in some order. There are two types of traversal.

1. Breadth First Search (BFS).2. Depth First Search (DFS).

Difference between Tree Traversal and Graph Traversal

1. Graph Traversal is different from Tree Traversal because there is no root or first node in a graph, hence the traversal can start from any node.

2. In tree all the nodes are traversed from the root but in graph, the node which are reachable from starting node is alone traversed. So if we want to traverse all reachable nodes, we again have to select another starting node.

3. Tree have unique traversals (i.e.) In Inorder, there can be only one visited sequence but in graph, for the same technique of traversal, there can be different visited sequence.

1. Breadth First Search (BFS)

It is one of the simplest algorithms for searching a graph. It is implemented with the help of Queue ADT it computes & produces a “Breadth First Tree” with root S (starting node) that contains all reachable vertices.

It is called BFS hence it expands or discovers the vertices uniformly across the breadth of frontier (i.e.) it discovers all vertices at distance K from S before discovery any vertices at distance K+X.

To keep track of the progress, BFS algorithm colors each vertex as white, gray or black. Initially all the vertices are white colored. When it is enter into the Queue, its color becomes gray. Once a vertex is added to the visited sequence, then the vertex color becomes black. For each vertex maintain its color, distance (d) & its predecessor (P) vertex. If it is non-weighted graph then edge weight will be considered as 1.

Procedure1. Take any one vertex as a source or starting vertex say ‘S’ and also make an empty

queue.2. For all vertex color them as white, assign distance as infinity and predecessor is Nil

except the source vertex. 3. Color the source vertex as gray, make that distance is 0 and predecessor is Nil.4. Enqueue the source vertex onto the Queue.5. Repeat the following step until Queue is empty.6. Dequeue the vertex from Queue and say it as vertex ‘U’.7. For all adjacent vertex ‘V’ of ‘U’, if the color of vertex as white then change it as

gray, Update the distance as distance(u) + distance(u to v), and assign ‘U’ as a predecessor of ‘V’ also enqueue the vertex V on to the queue.

8. Color the vertex ‘U’ as black and update the ‘U’ in visited sequence.For Example,

Data Structures Using C 229

Page 8: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

111Visited Order V=

1 2

3 4

5

6 8

7

B G W W

G W W W

Q 321

4

1 2

3 4

5

6 8

7

B B W W

G G W W

Q 221

3

V 121

2V 121

V 121

2 3 4

Q 521

6

1 2

3 4

5

6 8

7

B B G W

B B G W

V 121

2 3

Q 421

1 2

3 4

5

6 8

7

B B W W

B G W W

Graphs ADT

Data Structures Using C 230

Take S =’1’ vertex & Queue becomes Q =

Page 9: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

1

1 1 1

1 1

1

Q 721

8

1 2

3 4

5

6 8

7

B B B G

B B B G

V 121

2 3 4 5 6

Q 621

7

1 2

3 4

5

6 8

7

B B B G

B B G W

V 121

2 3 4 5

Q 8

1 2

3 4

5

6 8

7

B B B B

B B B G

V 121

2 3 4 5 6 7

Q

1 2

3 4

5

6 8

7

B B B B

B B B B

V 121

2 3 4 5 6 7 8

1 2

3 4

5

6 8

7

Graphs ADT

Output of BFS:

Pseudo codeBFS(G,S)

Data Structures Using C 231

BFS Spanning Tree output: Single Path from vertex ‘1’ to every other.

Vertex Predecessor Distance1 (Source) Nil 0

2 1 13 1 14 2 25 4 36 4 37 5 48 6 4

Page 10: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

Graphs ADT

For each vertex u except the source vertex {S}Do color[u]=white

d[u]=∞p[u]=Nil

color[S]=grayd[S]=0p[S]=NilEnqueue (Q,S)While Q ! = EMPTY

Do u=Dequeue(Q)For each v belong to Adj[u]

Do if color[V]=white thencolor[V]=grayd[v]=d[u]+1p[v]=uenqueue(Q,V)

color[u]=Black The total running time of BFS(G,S) is O(V+E).Note: DFS will be discussed later in this chapter.

9.6. TOPOLOGICAL SORT It is a linear ordering of vertices in a directed acyclic graph such that if there is a

path from Vi to Vj, then Vj appears after Vi in the ordering. Topological ordering is not possible, if the graph has a cycle. Because ‘V’ precedes

‘W’ and ‘W’ precedes ‘V’ if it has cycle.

Algorithm1. Find the indegree for every vertex and make empty queue with size of no.of vertex.2. Enqueue the vertices V on queue whose indegree is ‘0’ and not in topological order.3. Dequeue the vertex V & write it in topological order. Decrement the indegree of all

its adjacency vertices by one4. Repeat the process from step 3 until the queue becomes empty.5. The topological ordering is the order in which the vertices dequeued.

Pseudo codeTopsort(Graph G){Queue Q;int counter=0;Vertex V, W;Make empty(Q) with a size of Num_Vertices;For each vertex V If (Indegree[V]= =0) Enqueue(V,Q);

Data Structures Using C 232

Page 11: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A

CB

D

V1 V2

V3

V4 V5

Graphs ADT

While(! Is Empty(Q)) { V=Dequeue(Q); Toporder[++ counter]=V; For each W not in Toporder and adjacent to V

Indegree[W]=Indegree[W]-1; If(Indegree[W]= =0) Enqueue(W,Q);}If(counter!=Num_Vertices) Error(“Graph has a cycle”);DisposeQueue(Q); }

For Example

Given graph Equivalent Adjacency matrix

Vertex Indegree of vertex after a each pass (or) iterationInitial Pass 1 Pass 2 Pass 3 Pass 4

A 0 0 0 0 0B 1 0 0 0 0C 2 1 0 0 0D 2 2 1 0 0

In Queue A B C D -Dequeued (Or) Topological Order - A B C D

The topological ordering (or sorting) for the above graph is A, B, C & D

Example 2:

Data Structures Using C 233

A B C DA 0 1 1 0B 0 0 1 1C 0 0 0 1D 0 0 0 0

V1 V2 V3 V4 V5V1 0 1 1 1 0V2 0 0 1 0 1V3 0 0 0 0 0V4 0 0 1 0 1

V5 0 0 1 0 0

Page 12: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

Graphs ADT

Given graph Equivalent Adjacency matrixVertex Indegree of vertex after a each pass (or) iteration

Initial Pass 1 Pass 2 Pass 3 Pass 4 Pass 5V1 0 0 0 0 0 0V2 1 0 0 0 0 0V3 4 3 2 1 0 0V4 1 0 0 0 0 0V5 2 2 1 0 0 0In Queue V1 V2,V4 V4 V5 V3 -Dequeued (Or) Topological Order

- V1 V2 V4 V5 V3

The topological ordering for the above graph is V1, V2, V4, V5, and V3.

9.7. DEPTH FIRST SEARCH (DFS) AND APPLICATIONS DFS works by selecting one vertex V of ‘G’ as a start vertex; V is marked as

visited. Then each unvisited vertex adjacent to V is searched in turn using depth first search recursively. This process continues, until a dead end (i.e. a vertex with no unvisited vertices are encountered). At a dead end, the algorithm backtracks to one edge and continues visiting unvisited vertices from there. The algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in the same connected component as the starting vertex have been visited. If an unvisited vertex still remains, the DFS must be restarted as any one of the unvisited vertex considering as another start vertex. Distance & predecessor of vertex will be calculated as like in BFS.

Algorithm1. Choose any node in the graph. Designate it as the search node and mark it as visited.2. Find a node adjacent to the search node that has not been visited yet. Designate this

as the new search node and mark it as visited.3. Repeat the step 2 using the new search node.4. If the search node has no unvisited adjacent, backtrack to its parent and designate it

as the new search node and continue from step 2.5. If the graph still contains unvisited nodes, choose any node that has not been visited

and repeat step 1 through step 4

Routine for Depth First Search void DFS(Vertex V) { visited[V]=True; for each W adjacent to V if(!visited[W])

Data Structures Using C 234

Page 13: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A B

DC

A

B

D

C

A

B

D

A

B

B D

EC

A F

5

6

2

4

5

3

3

2

3

Graphs ADT

DFS(W); }

Example: 1 Adjacency Matrix Representation

1.Let ‘A’ be the source vertex. Mark it to be visited. 2.Find the immediate adjacent unvisited vertex i.e. ‘B’. Mark it to be visited.3.From ‘B’, the next adjacent vertex is ‘D’ and mark it to be visited.4.From ‘D’, the next unvisited vertex is ‘C’ and mark it to be visited.Finally, the Depth First Spanning Tree becomes,

Exercise Problem

APPLICATIONS OF DFS 1. Check whether the undirected graph is connected (or) not. 2. Check whether the undirected graph is bi-connected (or) not. 3. Check the acyclicity of directed graph.

Data Structures Using C 235

A B C D

A

B

C

D

A

0 1 1 11 0 0 11 0 0 11 1 1 0

Page 14: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A

EB D

C

A

B

C

D

A

B

C

D E

1 5

2 3 4

1 2 3 4 5

Graphs ADT

4. Check the existence of Euler Path and Circuit.

1. Undirected graphs is connected? An undirected graph is “connected” if and only if a depth first search starting from any node visits every node.Example: 1 Adjacency matrix

We start at vertex ‘A’. Mark it as visited and call DFS(B) recursively, it marks ‘B’ as visited and calls DFS(C) recursively. It marks ‘C’ as visited and calls DFS(D) recursively.

DFS(D) marks ‘D’ as visited.DFS(D) sees A,B,C as marked. So no recursive call is made there and DFS(D) returns back to DFS(C).

DFS(C) calls DFS(E), where E is unseen adjacent vertex to C.

Since all the vertices starting from ‘A’ are visited, the above graph is said to be connectedExample: 2

Adjacency matrix

Data Structures Using C 236

B C D E

A

B

C

D

A

E

Solid Line: Tree edgeDashed Line: Back edge

0 1 0 1 11 0 1 1 00 1 0 1 11 1 1 0 01 0 1 0 0

Page 15: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

1

2

3

4

5

1

2

3

4

.

5

DFS Forest

1

2

3

2. 1

2

3

4

3.1

2

B D

A C E

A

B

C

2. A

B

C

D

3. A

B

C

D

4.

E

A

B

Graphs ADT

We start at a vertex ‘1’1.

Here the vertex ‘4’ is not adjacent with any vertex. So DFS call terminates and again started with an unseen vertex ‘5’ to perform the traversal. Therefore the above graph is not connected.2. Acyclicity of a Directed Graph After DFS algorithm applied for a graph, if the DFS tree has any back edges, then the graph is called “cyclic graph” otherwise “acyclic”.

Example: 1

1.

So the given graph has cyclic, because the DFS tree has one back edge.

9.7.1. Bi-Connected Graph

Data Structures Using C 237

Start at 5

0 1 1 0 00 0 1 0 00 0 0 1 00 0 0 0 00 0 1 1 0

Page 16: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A B

C D F

EG

A B

C D F

EG

A B

C D F

EG

Graphs ADT

A connected undirected graph is bi-connected if there is no articulation point in th graph (it is a vertex whose removal disconnects the graph into two or more sub graphs). DFS provides a linear time algorithm to find all articulation points in a connected graph.

Example:

In above graph, removal of ‘C’ vertex will disconnect ‘G’ from the graph. Similarly, removal of ‘D’ vertex will disconnect E and F from the graph. Therefore ‘C’ and ‘D’ are articulation points. Hence the graph is not bi-connected.

Algorithm1. Perform Depth First Search, starting at any vertex.2. Number the vertex as they are visited, as Num(V).3. Compute the lowest numbered vertex for every vertex V in the DFS spanning tree

with respect to its post order traversal, which we call as low(W). Low(V) is the minimum of the following.

(i) Num(V) (ii) The lowest Num(W) among all back edges(V,W) (iii) The lowest Low(W) among all tree edges(V,W)

4. (i)The root is an articulation if and only if it has more than one child. (ii)Any vertex ‘V’ other than root is an articulation point if and only if ‘V’ has some child W such that low(W) ≥ Num(V).The time taken to compute this algorithm on a graph is O(|E|+|V|).

For any edge (V,W), we can tell whether it is a tree edge (or) back edge merely by checking Num(V) and Num(W). Take an edge (V,W), if Num(V) < Num(W), then the edge is a tree edge else back edge.

Data Structures Using C 238

Page 17: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A

B

C

D

E

F

G

Graphs ADT

Num(V)=1 Num(W)=2 Pseudo Code Void Assign Low (Vertex V)

{Vertex W;Low [V] = Num [V];for each W adjacent to V{

if(Num[W] > Num[V]) /*Forward edge*/{

Assign Low(W);if( Low[W] >=Num[V]) printf (“%c is an articulation point \n”,V);

Low[V]=Min(Low[v],Low[W]);}

elseif( parent[v]!=W) /*Back edge*/

Low[V]=Min(Low[V],Num[W]);

} }Low can be computed by performing a post order traversal {F, E, D, G, C, B, A} of the DFS tree. For the above graph the DFS tree becomes,Step 1: (Derive the DFS tree from the given graph)

Step 2 & 3: (calculate Num & Low value for each vertex in DFS tree)Low(F) =Min(Num(F),Num(D)) { there is no tree edge and only one back

edge}=Min(6,4) = 4

Data Structures Using C 239

Page 18: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A

B

C

D

E

F

G

Num: 1, Low: 1

Num: 2, Low: 1

Num: 3, Low: 1

Num: 7, Low: 7Num: 4, Low: 1

Num: 5, Low: 4

Num: 6, Low: 4

Graphs ADT

Low(E) =Min(Num(E),Low(F)) { there is no back edge}=Min(5,4) = 4Low(D) =Min(Num(D),Low(E),min(Num(B),Num(A)) {there is back edge and

a tree edges}=Min(4,4,1) =1Low(G) =Min(Num(G))=7 {there is no tree and back edges}Low(C) =Min(Num(C),min(Low(D),Low(G)),Num(A)) { there are 2 tree

edges}=Min(3,1,1) = 1Low(B) =Min(Num(B),Low(C))

=Min(2,1) = 1Low(A) =Min(Num(A),Low(B))=Min(1,1) =1

Step: 4 (Identify the articulation points in DFS tree)For vertex A i.e. root node, it has only one child. Therefore it is not an articulation point.For B, the child is C, Low(C) ≥ Num(B) => 1≥2 it is not true. So B is not an articulation point.For vertex C, the children are D & G,

Low(D) ≥ Num(C) => 1≥3 it is not true.Low(G) ≥ Num(C) => 7≥3 it is true. Therefore the vertex C is an articulation point.

For vertex D, the child is E,Low(E) ≥ Num(D) => 4≥4 it is true. Therefore the vertex D is an articulation point.

Similarly calculate for the remaining vertices.Hence the given graph has two articulation points such as C & D, so we conclude that the graph is not a Bi-Connected graph.

9.7.2. Euler Circuit

Data Structures Using C 240

Page 19: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

Graphs ADT

A popular puzzle is to reconstruct the following figures using a pen, drawing each line exactly once. The pen may not be lifted from the paper while the drawing is being performed.

As an extra challenge, make the pen finish at the same point at which it started. This puzzle has a surprisingly simple solution.

The first figure can be drawn only if the starting point is the lower left- or right-hand corner, and it is not possible to finish at the starting point.

The second figure is easily drawn with the finishing point the same as the starting point, but the third figure cannot be drawn at all within the parameters of the puzzle.

We can convert this problem to a graph problem by assigning a vertex to each intersection which becomes,

Euler Circuit: Find a cycle in the graph that visits every edge exactly once. This problem is commonly referred to as an Euler circuit problem.Euler Tour: Find a path in the graph that visits every edge exactly once. This problem is commonly referred to as an Euler tour problem.

Necessary condition for the existence of an Euler circuit & Path1. Euler circuit is possible if and only if the graph is connected and each vertex has an

even degree (number of edges).2. Euler tour is possible if and only if exactly two vertices have odd degree, which

must visit every edge but need not return to its starting vertex. We start at any one of the odd degree vertex and finish at the other. If more than two vertices have odd degree, then an Euler tour is not possible.

AlgorithmHence we know the existence of an Euler circuit, the main problem is that we might

visit a portion of the graph and return to the starting point prematurely. 1. At first, find a small cycle with any vertex.2. Next find the first vertex on this path that has an untraversed edge, and perform

another depth-first search. This will give another circuit, which can be spliced into the original.

3. This is continued until all edges have been traversed.

Data Structures Using C 241

Page 20: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

Graphs ADT

For example,

Suppose we start at vertex 5, and traverse the circuit 5, 4, 10, 5. Then we are stuck, and most of the vertices are still untraversed.

We then continue from vertex 4, which still has unexplored edges. A depth-first search might come up with the path 4, 1, 3, 7, 4, 11, 10, 7, 9, 3, 4. If we splice this path into the previous path of 5, 4, 10, 5, then we get a new path of 5, 4, 1, 3, 7 ,4, 11, 10, 7, 9, 3, 4, 10, 5.

The next vertex on the path that has untraversed edges is vertex 3. A possible circuit would then be 3, 2, 8, 9, 6, 3. When spliced in, this gives the path 5, 4, 1, 3, 2, 8, 9, 6, 3, 7, 4, 11, 10, 7, 9, 3, 4, 10, 5.

Data Structures Using C 242

Page 21: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

Graphs ADT

On this path, the next vertex with an untraversed edge is 9, and the algorithm finds the circuit 9, 12, 10, 9. When this is added to the current path, a circuit of 5, 4, 1, 3, 2, 8, 9, 12, 10, 9, 6, 3, 7, 4, 11, 10, 7, 9, 3, 4, 10, 5 is obtained. As all the edges are traversed, the algorithm terminates with an Euler circuit.

The total work performed on the vertex search phase is O(|E|).With the appropriate data structures, the running time of the algorithm is O(|E| + |V|).

9.8. REVIEW QUESTIONS & EXERCISES

1. Define the following terms and give an example for each.

a) Graphb) Undirected and directed graphc) Weighted graph d) Symmetric graphe) Connected componentf) Articulation pointg) Biconnected graphh) Spanning tree i) Indegree and outdegreej) Planner graph

2. Give any four real applications that can be represented with graph?

3. Mention any four properties of graph?

4. Differentiate strongly connected graph and weakly connected graph?

5. How do you represent a graph in memory? Give an example.

6. Differentiate adjacency matrix and list representation?

7. Consider the following graph G. Suppose nodes are stored in memory array

as X, Y, Z, S, T.

a) Find the adjacency matrix

b) Find the adjacency list

S T

X Z

Data Structures Using C 243

Page 22: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A

D

B

C

2

5

3

4

16

Graphs ADT

Y

8. The following is the adjacency matrix A of an undirected graph G. Draw the

graph and find its adjacency list representation.

A =

9. Write a procedure which determines whether or not, G is an undirected graph.

10. Is tree a graph? Is a graph a tree? Justify your answer.

11. What are the basic operations on graphs?

12. Describe breadth first and depth first search?

13. Write the procedure for BFS and DFS?

14. BFS is implemented using stack or queue? Justify.

15. DFS is implemented using stack or queue? Justify.

16. What is meant by topological ordering of vertices?

17. Prove that in finite graph the number of vertices of odd degree is always ever?

18. Draw an undirected graph with 6 vertices, 9 edges and 2 loops?

19. Perform BFS and DFS for the following graph?

2 3 6

2 9 2

1 8 3

20. Draw a digraph with 5 vertices,8 edges, path between every pair of vertices?

21. Answer the questions for the graph given below:

a) What are the neighbours of 4, 5?

b) Mark a path between 1-4, 2-6, 3-5. Give the length of each path?

c) Are there any cycles in the graph? If so list them.

d) Identify the connected component of the graph.

Data Structures Using C 244

0 1 1 01 0 0 10 0 0 1

0 1 1 0

Page 23: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

2

4

5

3

6

1

2 3

56

1 4

7

Graphs ADT

22. Draw an acyclic graph with five vertices?

23. Answer the questions with respect to the following digraph:

a) What is the indegree of vertices 1, 3 and 5.

b) What is the outdegree of vertices 2, 4 and 6.

c) Is the graph is strongly connected?

d) Represent its adjacency matrix.

e) Represent its adjacency list.

24. Draw the graphs corresponding to the following matrices:

0 1 0 0 1

1 0 1 1 1

1 1 0 0 1

1 0 0 0 1

1 0 1 1 0

2.

Data Structures Using C 245

Page 24: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

1

65

43

2

2

3

4 6

5

7

1

2 3

56

1 4

7

1

7

3

5

62

Graphs ADT

25. For the graph in Q24, draw the spanning trees.

26. For the following graph draw the DFS spanning tree.

27. Find the shortest path between vertex 1 and other vertices of the graph in Q21?

28. Can depth first search be used to find the connected component of a graph?

29. Find the shortest path between vertex 2 and other vertices of the graph

given below.8

10 6 11

8 12 5 10 6 13 5

5 8 9 9 6 13 15

7 10 8 7 10

13 20 6

30. What is the number of distinct simple graph with three nodes?

31. List the different algorithms used for implementing the MST?

32. Find the minimum spanning tree for the graph given below. Show the tree

after each step is Krushal’s algorithm. 8

Data Structures Using C 246

0 10 0 12 5

4 0 8 9 11

0 13 0 0 0

15 0 13 0 6

8 0 5 2 0

Page 25: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

2 3

56

1 4

7

1

2 3

5 6

4

7

8

8

4

5

12

6

7 3

8

4

5

12

6

7 3

Graphs ADT

10 6 21 9 12 5

9 9 20 6

5 25 17 5 15

12 7 19 7 7 10

15 13

33. Repeat Q29 using prim’s algorithm?

34. Mark the articulation points, if any in the following graph and mark whether

it is a bi-connected graph or not.

35. List some applications of DFS?

Data Structures Using C 247

Page 26: anandhavalli.files.wordpress.com  · Web viewThe algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in

A B

C D

EF

A B

C D

A B

C D

E

A B

C D

E

Graphs ADT

36. How do you verify the acyclic of a digraph?

37. Define Euler circuit and Euler path with an example?

38. What are the necessary condition for the existence of Euler circuit and Euler

path?

39. Write a pseudo code to find the Euler circuit and path for a given graph?

40. Mention the Euler circuit and path for the graph given below.

41. Find the Euler path for the following Graphs.

Data Structures Using C 248