Sei sulla pagina 1di 33

Chapter 11- Graphs

Graphs 11

11.1 Definition
11.2 Representation of Graphs
11.3 Topological Sort
11.4 Shortest-Path Algorithms
11.5 MinimumSpanning Tree
11.6 Graph Traversals
11.7 Applications of graph
11.8 Summary
11.9 Key Terms
11.10 Review Questions

195
Chapter 11- Graphs

Objectives
To understand the concepts of graph and its applications
To solve the problem of finding the shortest path using dijkstra's algorithm
To construct the minimum spanning tree using Prim's and Kruskals's algorithm

11.1. Definition

A graph consists of a set of nodes (or Vertices) and a set of arc (or edges). Each arc in a graph
is specified by a pair of nodes. A node is incident to an arc x if n is one of the two nodes in the
ordered pair of nodes that constitute x. The degree of a node is the number of arcs incident to
it. The indegree of a node n is the number of arcs that have n as the head, and the outdegree of
n is the number of arcs that have n as the tail. Loop is an edge that connects a vertex to itself.

The graph is the nonlinear data structure. The graph shown in the figure 11.1 represents 7
vertices and 12 edges. The Vertices are {1,2,3,4,5,6,7} and the arcs are{(1,2),(1,3),
(1,4),(2,4),(2,5),(3,4),(3,6),(4,5),(4,6),(4,7),(5,7),(6,7)}. Node(4) in figure has indegree 3,
outdegree 3 and degree 6.

Figure 11.1 – Example Graph

A directed graph is acyclic if it has no cycles. A directed acyclic graph is sometimes referred to
by its abbreviation, DAG.
An undirected graph is connected if there is a path from every vertex to every other vertex. A
directed graph with this property is called strongly connected. If a directed graph is not strongly
connected, but the underlying graph (without direction to the arcs) is connected, then the graph is
said to be weakly connected. A complete graph is a graph in which there is an edge between
every pair of vertices.

196
Chapter 11- Graphs

11.2. Representation of Graphs

Two common ways to represent graphs on a computer are as an adjacency list or as an adjacency
matrix.
11.2.1. Adjacency Matrix Representation
One simple way to represent a graph is to use a two-dimensional array. This isknown as an
adjacency matrix representation. For each edge (u, v), we set a[u][v]= 1; otherwise the entry in
the array is 0. If the edge has a weight associatedwith it, then we can set a[u][v] equal to the
weight and use either a very largeor a very small weight as a sentinel to indicate nonexistent
edges.
AnadjacencymatrixofagraphG=(V,E)(letV={v1 ,v2 .....,vn})isanXn matrix A,such that
A[i, j]=1 ifthereis edgebetween vi and v j.
0 other wise

Figure 11.2 – Adjacency Matrix

11.2.2. AdjacencyListRepresentation
Adjacency lists are the standard way to represent graphs. For each vertex, we keep a list ofall
adjacent vertices, whichcanberepresentedeitherbylinkedlistorarray. The space requirement is then
O|E| + |V|).
An example is given below.

Figure 11.3 – Adjacency List

197
Chapter 11- Graphs

11.3. Topological Sort

 Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as
a sequence, such that no vertex appears in the sequence before its predecessor.
 One way to find a topological sort is to consider in-degrees of the vertices.The first vertex
must have in-degree zero.
 Every DAG must have at least one vertex with in-degree zero. Topological sort is not
unique.
 The following are all topological sort of the graph below:

s1 = {a, b, c, d, e, f, g, h, i}
s2 = {a, c, b, f, e, d, h, g, i}
s3 = {a, b, d, c, e, g, f, h, i}
s4 = {a, c, f, b, e, h, d, g, i}
etc.

Figure 11.4 – Example graph and its ordering for Topological Sort

Strategy of Topological Sort


First, the indegree is computed for every vertex. Then all vertices of indegree 0 are placed on an
initially empty queue. While the queue is not empty, a vertex v is removed, and all edges
adjacent to v have their indegree’s decremented. A vertex is put on the queue as soon as its
indegree falls to 0. The topological ordering then is the order in which the vertices dequeue.
Figure 11.6 shows the status after each phase for the graph in 11.5.

Example

Figure 11.5- An acyclic graph

198
Chapter 11- Graphs

In degree before Dequeue

Vertex 1 2 3 4 5 6 7
----------------------------------------------------------------------------
V1 0 0 0 0 0 0 0
V2 1 0 0 0 0 0 0
V3 2 1 1 1 0 0 0
V4 3 2 1 0 0 0 0
V5 1 1 0 0 0 0 0
V6 3 3 3 3 2 1 0
V7 2 2 2 1 0 0 0
---------------------------------------------------------------------------
Enqueue v1 v2 v5 v4 v3 v7 v6
---------------------------------------------------------------------------
Dequeue v1 v2 v5 v4 v3 v7 v6

Figure 11.6- Result of applying topological sort for the graph 11.5.

Algorithm

void topsort( graph G )


{
unsigned int counter;
vertex v, w;
for( counter = 0; counter < NUM_VERTEX; counter++ )
{
v = find_new_vertex_of_indegree_zero( );
if( v = NOT_A_VERTEX )
{
error("Graph has a cycle");
break;
}
top_num[v] = counter;
for each w adjacent to v
indegree[w]--;
}
}

199
Chapter 11- Graphs

Analysis of Topological Sort


The running time of topological sort is O (|V2|)

11.4 Shortest-Path Algorithms

The shortest path problem is the problem of finding a path between two vertices (or nodes) in a
graph such that the sum of the weights of its constituent edges is minimized. This is analogous to
the problem of finding the shortest path between two intersections on a road map: the graph's
vertices correspond to intersections and the edges correspond to road segments, each weighted
by the length of its road segment. The algorithm used to find the shortest path is,
 Unweighted Shortest Paths Algorithm
 Dijkstra's Algorithm (Weighted Shortest Paths Algorithm)

11.4.1.Unweighted Shortest Paths Algorithm

Figure 11.7- An Unweighted graph

Let G(V,E) be a unweighted graph. Given a source vertex s, find the shortest path to all other
vertices.The algorithm uses Breadth-first search in the graph: Take a vertex and examine all
adjacent vertices. Do the same with each of the adjacent vertices

Data structures needed


 A distance table with rows for each vertex w and two columns:
dv - Distance from source vertex
pv - Path - contains the name of the vertex that precedes w in the path from s to w.
 A queue used to implement breadth-first search.It contains vertices whose distance from
the source node has been computed and their adjacent vertices are to be examined.
 Adjacency lists :
V1: V2, V4
V2: V4, V5
V3: V1, V6
V4: V3, V5, V6, V7

200
Chapter 11- Graphs

V5: V7
V6: -
V7: V6
 Let s = V3. The distance from V3 to V3 is 0.
Initially the distances to all other nodes are not computed,
and we initialize the first column in the distance table for all vertices (except V3) with -1

Steps
1. Store s in a queue and set the distance to s to be 0 in the Distance Table.
Initialize the distances to all other vertices as "not computed",
e.g. using a sentinel value ∞.
2. While there are vertices in the queue:
a. Read a vertex v from the queue
b. For all adjacent vertices w:
 If distance to w is ∞ (not computed) do:
 Make distance to w equal to (distance to v) + 1
 Make path to w equal to v
 Append w to the queue

Algorithm

void unweighted( TABLE T )


{
QUEUE Q;
vertex v, w;
Q = create_queue( NUM_VERTEX );
make_null( Q );
enqueue( s, Q );
while( !is empty( Q ) )
{
v = dequeue( Q );
T[v].known = TRUE;
for each w adjacent to v
if( T[w].dist = INT_MAX )
{
T[w].dist = T[v].dist + 1;

201
Chapter 11- Graphs

T[w].path = v;
enqueue( w, Q );
}
}
dispose_queue( Q );
}

Initial state v3 Dequeued v1 Dequeued v6 Dequeued

v known dv pv v known dv pv v known dv pv v known dv pv


v1 0 ∞ 0 v1 0 1 v3 v1 1 1 v3 v1 1 1 v3
v2 0 ∞ 0 v2 0 ∞ 0 v2 0 2 v1 v2 0 2 v1
v3 0 0 0 v3 1 0 0 v3 1 0 0 v3 1 0 0
v4 0 ∞ 0 v4 0 ∞ 0 v4 0 2 v1 v4 0 2 v1
v5 0 ∞ 0 v5 0 ∞ 0 v5 0 ∞ 0 v5 0 ∞ 0
v6 0 ∞ 0 v6 0 1 v3 v6 0 1 v3 v6 1 1 v3
v7 0 ∞ 0 v7 0 ∞ 0 v7 0 ∞ 0 v7 0 ∞ 0

Q: v3 Q: v1,v6 Q: v6,v2,v4 Q: v2,v4


v2 Dequeued v4 Dequeued v5 Dequeued v7 Dequeued

v known dv pv v known dv pv v known dv pv v known dv pv


v1 1 1 v3 v1 1 1 v3 v1 1 1 v3 v1 1 1 v3
v2 1 2 v1 v2 1 2 v1 v2 1 2 v1 v2 1 2 v1
v3 1 0 0 v3 1 0 0 v3 1 0 0 v3 1 0 0
v4 0 2 v1 v4 1 2 v1 v4 1 2 v1 v4 1 2 v1
v5 0 3 v2 v5 0 3 v2 v5 1 3 v2 v5 1 3 v2
v6 1 1 v3 v6 1 1 v3 v6 1 1 v3 v6 1 1 v3
v7 0 ∞ 0 v7 0 ∞ v4 v7 0 3 v4 v7 1 3 v4

Q: v4,v5 Q: v5,v7 Q: v7 Q: empty

Figure 11.8 – Process ofUnweighted shortest-path algorithm for 11.7

Complexity
The complexity for unweighted shortest path algorithm is:O(|E| + |V|). If we use
matrixrepresentation the complexity is O(|V|2), because we need to read an entire row in the
matrix of length |V| in order to find the adjacent vertices for a given vertex.

202
Chapter 11- Graphs

11.4.2 Dijkstra’s Algorithm


Dijkstra's algorithm, is a graph search algorithm that solves the single-source shortest path
problem for a graph with nonnegative edge path costs, producing a shortest path tree.
The method works by maintaining a set S of vertices for which shortest path from source v0 is
found. Initially, S is empty and shortest distance to each vertex from source is infinity. The
algorithm repeatedly selects a vertex vi in V-S to which path from source is shorter than other
vertices in V-S, adds to S, and estimates the shortest distance to other vertices from vi.

Steps
1. Store s in a priority queue with priority 0 and set distance to s to be 0 in the Distance
Table.
Initialize the distances to all other vertices as "not computed",
e.g. using a sentinel value -1.
2. While there are vertices in the queue:
a. DeleteMin a vertex v from the queue
b. For all adjacent vertices w do:
 Compute new distance d= (distance to v) + weight(v,w)
 If old distance (the distance stored in the Distance Table) is -1
(not computed) do:
 store new distance d in Distance Table
 insert w in the priority queue with priority d
 store path = v
 If old distance > new distance d
 Update old distance = new distance d
 Update priority of vertex w to be d
(this is done by updating the priority of an element in the queue -
decreaseKey operation. Complexity O(log|V|))
 Update path to w to be v
Example

Figure 11.9 – Example Graph for Dijkstra’s Algorithm

203
Chapter 11- Graphs

v known dv pv v known dv pv v known dv pv v known dv pv


v1 0 0 0 v1 1 0 0 v1 1 0 0 v1 1 0 0
v2 0 ∞ 0 v2 0 2 v1 v2 0 2 v1 v2 1 2 v1
v3 0 ∞ 0 v3 0 ∞ 0 v3 0 3 v4 v3 0 3 v4
v4 0 ∞ 0 v4 0 1 v1 v4 1 1 v1 v4 1 1 v1
v5 0 ∞ 0 v5 0 ∞ 0 v5 0 3 v4 v5 0 3 v4
v6 0 ∞ 0 v6 0 ∞ 0 v6 0 9 v4 v6 0 9 v4
v7 0 ∞ 0 v7 0 ∞ 0 v7 0 5 v4 v7 0 5 v4

v1 is declared v4 is declared v2 is declared


Initial Configuraion
known known known

v known dv pv v known dv pv v known dv pv v known dv pv


v1 1 0 0 v1 1 0 0 v1 1 0 0 v1 1 0 0
v2 1 2 v1 v2 1 2 v1 v2 1 2 v1 v2 1 2 v1
v3 1 3 v4 v3 1 3 v4 v3 1 3 v4 v3 1 3 v4
v4 1 1 v1 v4 1 1 v1 v4 1 1 v1 v4 1 1 v1
v5 0 3 v4 v5 1 3 v4 v5 1 3 v4 v5 1 3 v4
v6 0 8 v3 v6 0 8 v3 v6 0 6 v7 v6 1 6 v7
v7 0 5 v4 v7 0 5 v4 v7 1 5 v4 v7 1 5 v4

v3 is declared known v5 is declared known v7 is declared known v6 is declared known

Figure 11.10 – Processing steps for Dijkstra’s algorithm for 11.9

Algorithm

Dijkstra_shortest_path(G, v_0)
For each vertex vi in V do
dist[vi]=infinity
distance[v0]=0;
for each vertex v_i in V do
insert(Q,vi)
S = empty
while (Qnot empty) do
vi=Min(Q)
for each vertex vj adjacent to vi do
distance[vj]=Minimum(distance[vj],distance[vi]+w[vi,vj])

204
Chapter 11- Graphs

Figure 11.11- Stages of Dijkstra's algorithm

Difference between unweighted and weighted shortest path algorithm (Dijkstra’s


algorithm)
 The adjacency lists contain in addition the weights of the edges
 Instead of ordinary queue, a priority queue is used (the distances being the priorities)
and the vertex with the smallest distance is selected for processing
 The distance to a vertex is measured by the sum of the weights of the edges that
constitute the path from the source vertex.
 Distances are subjected to adjustments - in case the newly computed distance is smaller.
Complexity
 Complexity: O(|E|log|V| + |V|log|V|) = O((|E| + |V|)log(|V|))

205
Chapter 11- Graphs

 Each vertex is stored only once in the queue - max elements = |V|
 The deleteMin operation is O( |V|log|V| )
 The decreaseKey operation is O(log|V|) (a search in the binary heap).
It might be performed for each examined edge - O(|E|log|V|).

11.5. MinimumSpanning Tree

The minimum spanning tree of a weighted graph is a set of edges of minimum total weight
which form a spanning tree of the graph. When a graph is unweighted, any spanning tree is a
minimum spanning tree.

Definition
A tree is a connected graph without cycles is called Minimum spanning Tree.
Properties of Trees
° A graph is a tree if and only if there is one and only one path joining any two of its vertices.
° A connected graph is a tree if and only if every one of its edges is a bridge.
° A connected graph is a tree if and only if it has N vertices and N; 1 edges.
Other Definitions
° A subgraph that spans (reaches out to) all vertices of a graph is called a spanning subgraph.
° A subgraph that is a tree and that spans (reaches out to) all vertices of the original graph is
called a spanning tree.
° Among all the spanning trees of a weighted and connected graph, the one (possibly more) with
the least total weight is called a minimum spanning tree (MST).

Figure11.12 – Weighted Graph and its Minimum Spanning Tree


The cost of the laying the above pipeline connecting these oil wells is 20. Any other way of
connecting increases the cost. Resultant graph is a tree which spannes all vertices of the graph.
Hence called spanning tree. The spanning tree with minimumcostis called MinimumSpanning
tree (MST).
Implementation of Minimum spanning trees
 Prim’s algorithm
 Kruskal’s algorithm

206
Chapter 11- Graphs

11.5.1. Prims’ algorithm


Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a
connectedweightedundirected graph. This means it finds a subset of the edges that forms a tree
that includes every vertex, where the total weight of all the edges in the tree is minimized.

Figure 11.13 – Graph and its Minimum Spanning Tree (Prim’s &Kruskal’s)

v known dv pv v known dv pv v known dv pv v known dv pv


v1 0 0 0 v1 1 0 0 v1 1 0 0 v1 1 0 0
v2 0 ∞ 0 v2 0 2 v1 v2 0 2 v1 v2 1 2 v1
v3 0 ∞ 0 v3 0 4 v1 v3 0 2 v4 v3 0 2 v4
v4 0 ∞ 0 v4 0 1 v1 v4 1 1 v1 v4 1 1 v1
v5 0 ∞ 0 v5 0 ∞ 0 v5 0 7 v4 v5 0 7 v4
v6 0 ∞ 0 v6 0 ∞ 0 v6 0 8 v4 v6 0 8 v4
v7 0 ∞ 0 v7 0 ∞ 0 v7 0 4 v4 v7 0 4 v4

Initial Configuration v1 is declared known v4 is declared known v2 is declared known

v known dv pv v known dv pv v known dv pv v known dv pv


v1 1 0 0 v1 1 0 0 v1 1 0 0 v1 1 0 0
v2 1 2 v1 v2 1 2 v1 v2 1 2 v1 v2 1 2 v1
v3 1 2 v4 v3 1 2 v4 v3 1 2 v4 v3 1 2 v4
v4 1 1 v1 v4 1 1 v1 v4 1 1 v1 v4 1 1 v1
v5 0 7 v4 v5 0 6 v7 v5 0 6 v7 v5 1 6 v7
v6 0 5 v3 v6 0 1 v7 v6 1 1 v7 v6 1 1 v7
v7 0 4 v4 v7 1 4 v4 v7 1 4 v4 v7 1 4 v4

v3 is declared known v7 is declared known v6 is declared known v5 is declared known

Figure 11.14- Processing Steps– Prim’s Algorithm

207
Chapter 11- Graphs

Algorithm
The algorithm continuously increases the size of a tree, one edge at a time, starting with a tree
consisting of a single vertex, until it spans all vertices.

 Input: A non-empty connected weighted graph with vertices V and edges E


(the weights can be negative).
 Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V, Enew = {}
 Repeat until Vnew = V:
o Choose an edge (u, v) with minimal weight such that u is in Vnew and v is not
(if there are multiple edges with the same weight, any of them may be picked)
o Add v to Vnew, and (u, v) to Enew
 Output: Vnew and Enew describe a minimal spanning tree

Figure 11.15 – Stages of Prim’s algorithm


Time complexity
Prim's algorithm can be shown to run in time O(E log V) where E is the number of edges and V
is the number of vertices.

208
Chapter 11- Graphs

11.5.2. Kruskals’ Algorithm


Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a
connectedweighted graph. This means it finds a subset of the edges that forms a tree that
includes every vertex, where the total weight of all the edges in the tree is minimized. If the
graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for
each connected component). Kruskal's algorithm is an example of a greedy algorithm.

An implementation of the algorithm can be done using a set data structure. Each component is
represented as a set. Add next least weighted edge (u,v), if u and v are in two different
components or sets. That is, make a bigger component by taking the union of the sets containing
u and v.

Figure 11.16 – Stages of Kruskal’s algorithm


Steps
 create a forest F (a set of trees), where each vertex in the graph is a separate tree
 create a set S containing all the edges in the graph
 while S is nonempty and F is not yet spanning
o remove an edge with minimum weight from S
o if that edge connects two different trees, then add it to the forest, combining two
trees into a single tree
o Otherwise, discard that edge.

209
Chapter 11- Graphs

At the termination of the algorithm, the forest forms a minimum spanning forest of the graph. If
the graph is connected, the forest has a single component and forms a minimum spanning tree.

Data Structure
 Make_SET(v): Create a new set whose only member is pointed to by v. Note that for
this operation v must already be in a set.
 FIND_SET(v): Returns a pointer to the set containing v.
 UNION(u, v): Unites the dynamic sets that contain u and v into a new set that is union
of these two sets.

Algorithm

KRUSKAL(G):
A=∅
foreach v ∈ G.V:
MAKE-SET(v)
foreach (u, v) ordered by weight(u, v), increasing:
if FIND-SET(u) ≠ FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)
return A

Analysis
The total running time of Kruskal’s Algorithm is O((V + E) log V), which is O(E log V) since
graph is simple and connected.

11.6. Graph Traversals

Many graph problems can be solved by visiting each and every vertex in a systematic manner.
There are two main methods to traversal graphs,
 DepthFirstSearch(DFS)
 BreadthFirstSearch (BFS)

11.6.1 DepthFirstSearch(DFS)
Depth-first search(DFS) is an algorithm for traversing or searching tree or graph data structures.
One starts at the root (selecting some node as the root in the graph case) and explores as far as
possible along each branch before backtracking.

210
Chapter 11- Graphs

Depth First Search (DFS) searches deeper into the problem space. Depth First Search uses last-in
first-out stack for keeping the unexpanded nodes. More commonly, depth-first search is
implemented recursively, with the recursion stack taking the place of an explicit node stack.

Steps
1.If the initial state is a goal state, quit and return success.
2.Otherwise, loop until success or failure is signaled.
a) Generate a state, say E, and let it be the successor of the initial state. If there is no
successor, signal failure.
b) Call Depth-First Search with E as the initial state.
c) If success is returned, signal success. Otherwise continue in this loop.

Algorithm

DFS (G)
fori =1 to n do
unvisited
parent[i]=NULL
fori =1 to n do
if(status[i] == unvisited)
DF-Travel(i)

DF-Travel(v )
status[v]=visited
for each vertexu adjacent to vdo
ifstatus[u]==unvisited then
parent[u]=v
DF-Travel (u)

Advantages of Depth-First Search


1. The advantage of depth-first Search is that memory requirement is only linear with
respect to the search graph. This is in contrast with breadth-first search which requires
more space. The reason is that the algorithm only needs to store a stack of nodes on the
path from the root to the current node.
2. The time complexity of a depth-first Search to depth d is O(b^d) since it generates the
same set of nodes as breadth-first search, but simply in a different order. Thus practically
depth-first search is time-limited rather than space-limited.

211
Chapter 11- Graphs

3. If depth-first search finds solution without exploring much in a path then the time and
space it takes will be very less.

Disadvantages of Depth-First Search


1. The disadvantage of Depth-First Search is that there is a possibility that it may go down
the left-most path forever. Even a finite graph can generate an infinite tree. One solution
to this problem is to impose a cutoff depth on the search. Although the ideal cutoff is the
solution depth d and this value is rarely known in advance of actually solving the
problem. If the chosen cutoff depth is less than d, the algorithm will fail to find a
solution, whereas if the cutoff depth is greater than d, a large price is paid in execution
time, and the first solution found may not be an optimal one.
2. Depth-First Search is not guaranteed to find the solution.
3. And there is no guarantee to find a minimal solution, if more than one solution exists.

Figure 11.17- Graph and its DFS

11.6.2 BreadthFirstSearch(BFS)
Breadth First Search (BFS) searches breadth-wise in the problem space. Breadth-First search is
like traversing a tree where each node is a state which may be a potential candidate for solution.
Breadth first search expands nodes from the root of the tree and then generates one level of the
tree at a time until a solution is found. It is very easily implemented by maintaining a queue of
nodes. Initially the queue contains just the root. In each iteration, node at the head of the queue is
removed and then expanded. The generated child nodes are then added to the tail of the queue.

Since it never generates a node in the tree until all the nodes at shallower levels have been
generated, breadth-first search always finds a shortest path to a goal. Since each node can be
generated in constant time, the amount of time used by Breadth first search is proportional to the
number of nodes generated, which is a function of the branching factor b and the solution d.
Since the number of nodes at level d is bd, the total number of nodes generated in the worst case
is b + b2 + b3 +… + bd i.e. O(bd) , the asymptotic time complexity of breadth first search.

212
Chapter 11- Graphs

Algorithm
1. Create a variable called NODE-LIST and set it to the initial state.
2. Loop until the goal state is found or NODE-LIST is empty.
a. Remove the first element, say E, from the NODE-LIST. If NODE-LIST was
empty then quit.
b. For each way that each rule can match the state described in E do:
i) Apply the rule to generate a new state.
ii) If the new state is the goal state, quit and return this state.
iii) Otherwise add this state to the end of NODE-LIST

Advantages of Breadth-First Search


Breadth first search will never get trapped exploring the useless path forever.
 If there is a solution, BFS will definitely find it out.
 If there is more than one solution then BFS can find the minimal one that requires less
number of steps.

Disadvantages of Breadth-First Search


 The main drawback of Breadth first search is its memory requirement. Since each level of
the tree must be saved in order to generate the next level, and the amount of memory is
proportional to the number of nodes stored, the space complexity of BFS is O(bd).
 If the solution is farther away from the root, breath first search will consume lot of time.

Figure 11.18- Graph and its Breadth First Search

Compare DFS and BFS

Key Terms DFS BFS


Depth-first search (DFS) is an Breadth-first search (BFS) is a graph
algorithm for traversing or searching search algorithm that begins at the root
Definition a tree, tree structure, or graph. node and explores all the neighboring

213
Chapter 11- Graphs

Intuitively, one starts at the root nodes. Then for each of those nearest
(selecting some node as the root in nodes, it explores their unexplored
the graph case) and explores as far as neighbor nodes, and so on, until it
possible along each branch before finds the goal.
backtracking.
Data structure Uses stack Uses queue
Applications are finding articulation Applications are finding minimum
Application points, Euler circuits spanning tree, Dijkstra’s algorithm

Example

11.7 Applications of Graph

 Undirected graph
 Bi connectivity
 Euler circuits
 Directed graph
 Finding strong components

11.7.1. Undirected Graph


An undirected graph is connected if and only if a Depth First Search of any graphs starting from
any node that visits every node. For example, consider the undirected graph shown in figure 11.19.

Figure 11.19 – Undirected Graph&its DFS

214
Chapter 11- Graphs

Construct the DFS for the graph starts from the vertex A, it travers to B, then C, and then D. From
D there is no further vertex to visit, because vertices A & B are already visited. So move
backwards to C and then visit E. Now the resultant graph is shown in figure 11.19. In this example,
all the vertices starting from A are visited, so the above graph is said to be connected. If the graph
is not connected, then processing all nodes and edges requires several calls DFS, and each
generates a tree. This entire collection is called depth first spanning forest.
11.7.2 Biconnectivity
A connected undirected graph is biconnected if there are no vertices whose removal disconnects
the rest of the graph. The graph in the example is biconnected.

Figure 11.20 –Graph Biconnected

If a graph is not biconnected, the vertices whose removal would disconnect the rest of the graph
are called as articulation points. These nodes are critical in many applications. The graph shown
in figure 11.21 is not biconnected. The removal of C would disconnect G, and the removal of D
would disconnect E and F, from the rest of the graph. In this example, vertices C & D are the
articulation points.

Figure 11.21 – Graph (Not Biconnected)

Depth-first search provides a linear-time algorithm to find all articulation points in a connected
graph.

215
Chapter 11- Graphs

 First, starting at any vertex, we perform a depth-first search and number the nodes as
they are visited.
 For each vertex v, we call this preorder number num(v).
 Then, for every vertex v in the depth-first search spanning tree, we compute the
lowest-numbered vertex, which we call low(v), that is reachable from v by taking zero
or more tree edges and then possibly one back edge (in that order). By the definition
of low, low(v) is the minimum of
1. num(v)
2. the lowest num(w) among all back edges (v, w)
3. the lowest low(w) among all tree edges (v, w)
The first condition is the option of taking no edges, the second way is to choose no tree edges
and a back edge, and the third way is to choose some tree edges and possibly a back edge. This
third method is succinctly described with a recursive call. Since we need to evaluate low for all
the children of v before we can evaluate low(v), this is a postorder traversal.

Algorithm

void assign_num( vertex v )


{
vertex w;
num[v] = counter++;
visited[v] = TRUE;
for each w adjacent to v
if( !visited[w] )
{
parent[w] = v;
assign_num( w );
}
}

/* assign low. Also check for articulation points */


void assign_low( vertex v )
{
vertex w;
low[v] = num[v];
for each w adjacent to v
{

assign_low( w );
216
Chapter 11- Graphs

if(num[w] >num[v] )
{
assign_low( w );
if( low[w] >= num[v] )
printf( "%v is an articulation point\n", v );
low[v] = min( low[v], low[w] );
}
else
if( parent[v] != w )
low[v] = min( low[v], num[w] );
}
}

void find_art( vertex v )


{
vertex w;
visited[v] = TRUE;
low[v] = num[v] = counter++;
for each w adjacent to v
{
if( !visited[w] )
{
parent[w] = v;
find_art( w );
if( low[w] >= num[v] )
printf ( "%v is an articulation point\n", v );
low[v] = min( low[v], low[w] );
}
else
if( parent[v] != w )
low[v] = min( low[v], num[w] );
}
}

217
Chapter 11- Graphs

Example

Figure 11.22 – Graph for finding articulation points

Step1: Construct DFS for the graph starting from the source vertex A. Then the resultant graph
will be

Figure 11.23 – DFS for the Graph in 11.22

Step 2: For each vertex v, we call this preorder number num(v). Assign the number for the
vertices in the order they are visited using DFS. Now the graph that has assigned a number for
the vertices as,

Figure 11.24 – Graph and its numbering

218
Chapter 11- Graphs

Step 3: Finding the low value for the vertices starting from the leaf node to the root node. For
any vertex v,
Low(v) is the minimum of
1. num(v)
2. the lowest num(w) among all back edges (v, w)
3. the lowest low(w) among all tree edges (v, w)
Take a vertex F for an example,
Low(F) = min { num(F), num(D)} /* since, there is no tree edge only one back edge
Low(F) = min { 6, 4} = 4
Then, take a vertex E,
Low(E) = min { num(E), low(F)} /* since, there is no back edge
Low(E) = min { 5, 4} = 4
Then take a vertex D,
Low(D) = min { num(D), low(E), num(A)} /* since, A is the back edge of D
Low(D) = min { 4, 4, 1 } = 1
Then take a vertex G before C.
Low(G) = min { num(G) } /* since, For G there is no tree edge and back edge
Low(G) = min { 7 } = 7

Figure 11.25 – Vertices with num& low

Now we take C,

219
Chapter 11- Graphs

Low(C) = min { num(C), low(D), low(G)}


Low(C) = min { 3, 1, 7 } = 1
Now we take C,
Low(C) = min { num(C), low(D), low(G)}
Low(C) = min { 3, 1, 7 } = 1

Similarly, find the low value for B and then A.


Low(B) = min { num(B), low(C) }
Low(B) = min { 2, 1 } = 1
For the root vertex A,
Low(A) = min { num(A), low(B) }
Low(A) = min { 1, 1 } = 1
Now, the resultant graph is shown in figure 11.25

Step 4: Apply an algorithm, finding the articulation points for the vertices that starts from the
root node to the leaf node. A, C & D are the vertices that satisfied the condition mentioned in the
algorithm. Now remove the root vertex from the set, we have the vertices C & D. So, for this
above graph, C & D are the articulation points.

11.7.3 Euler Circuits


 An Euler path is a path that uses every edge of a graph exactly once.
 An Euler circuit is a circuit that uses every edge of a graph exactly once.
 An Euler path starts and ends at different vertices.
 An Euler circuit starts and ends at the same vertex.
Task
Given an undirected or a directed graph, find a path or circuit that passes through each edge
exactly once.
Solution
First let's see the conditions for an undirected graph:
 An undirected graph has a eulerian circuit if and only if it is connected and each vertex
has an even degree (degree is the number of edges that are adjacent to that vertex).
 An undirected graph has a eulerian path if and only if it is connected and all vertices
except 2 have even degree. One of those 2 vertices that have an odd degree must be the
start vertex, and the other one must be the end vertex.
For a directed graph we have:
 A directed graph has a eulerian circuit if and only if it is connected and each vertex has
the same in-degree as out-degree.
 A directed graph has an eulerian path if and only if it is connected and each vertex except
2 have the same in-degree as out-degree, and one of those 2 vertices has out-degree with

220
Chapter 11- Graphs

one greater than in-degree (this is the start vertex), and the other vertex has in-degree
with one greater than out-degree (this is the end vertex).

Steps for undirected graphs


1. Start with an empty stack and an empty circuit (eulerian path).
- If all vertices have even degree - choose any of them.
- If there are exactly 2 vertices having an odd degree - choose one of them.
- Otherwise no Euler circuit or path exists.
2. If current vertex has no neighbors - add it to circuit, remove the last vertex from the stack
and set it as the current one. Otherwise (in case it has neighbors) - add the vertex to the
stack, take any of its neighbors, remove the edge between selected neighbor and that
vertex, and set that neighbor as the current vertex.
3. Repeat step 2 until the current vertex has no more neighbors and the stack is empty.
Note that obtained circuit will be in reverse order - from end vertex to start vertex.

Steps for directed graphs


1. Start with an empty stack and an empty circuit (eulerian path).
- If all vertices have same out-degrees as in-degrees - choose any of them.
- If all but 2 vertices have same out-degree as in-degree, and one of those 2 vertices has
out-degree with one greater than its in-degree, and the other has in-degree with
onegreater than its out-degree - then choose the vertex that has its out-degree with one
greater than its in-degree.
- Otherwise no euler circuit or path exists.
2. If current vertex has no out-going edges (i.e. neighbors) - add it to circuit, remove the last
vertex from the stack and set it as the current one. Otherwise (in case it has out-going
edges, i.e. neighbors) - add the vertex to the stack, take any of its neighbors, remove the
edge between that vertex and selected neighbor, and set that neighbor as the current
vertex.
3. Repeat step 2 until the current vertex has no more out-going edges (neighbors) and the
stack is empty.

Example

Figure 11.26 – Graph for Euler Path

221
Chapter 11- Graphs

Figure 11.27 – Solution for Euler Path

Figure 11.28 – Undirected Graph for Euler Circuit

Figure 11.29 – Solution for Euler Circuit (Graph that starts from 1 then moves towards
3,2,8,9,3,4,5,10,4,7,3,6,9,7,10,9,12,10,11,4,1)

The Criterion for Euler Paths


Suppose that a graph has an Euler path P.For every vertex v other than the starting and ending
vertices,the path P enters v the same number of times that it leaves v(say s times).Therefore,

222
Chapter 11- Graphs

there are 2s edges having v as an endpoint.Therefore, all vertices other than the two endpoints
ofP must be even vertices.
Complexity
The complexity of both algorithms is O(N+M), where N is the number of vertices and M is the
number of edges.

11.7.4 Directed Graph


Using the same strategy as with undirected graphs, directed graphs can be traversed in
lineartime, using depth-first search. If the graph is not strongly connected, a depth-first
searchstarting at some node might not visit all nodes. In this case we repeatedly perform depth-
firstsearches, starting at some unmarked node, until all vertices have been visited. As an
example,consider the directed graph in Figure 11.30.

Figure 11.30 – Directed Graph

Figure 11.31 – DFS of the graph in figure 11.30

The dashed arrows in the depth-first spanning forest are edges (v, w) for which w was
alreadymarked at the time of consideration. In undirected graphs, these are always back edges,

223
Chapter 11- Graphs

but, aswe can see, there are three types of edges that do not lead to new vertices. First, there
areback edges, such as (A, B) and (I, H). There are also forward edges, such as (C, D) and (C,
E),that lead from a tree node to a descendant. Finally, there are cross edges, such as (F, C)
and(G, F), which connect two tree nodes that are not directly related. Depth-first search
forestsare generally drawn with children and new trees added to the forest from left to right. In
adepth- first search of a directed graph drawn in this manner, cross edges always go from right
toleft.

One use of depth-first search is to test whether or not a directed graph is acyclic. The rule isthat a
directed graph is acyclic if and only if it has no back edges. (The graph above has backedges, and
thus is not acyclic.)

11.7.5 Finding strong components


A directed graph is called strongly connected if there is a path from each vertex in the graph to
every other vertex. In particular, this means paths in each direction; a path from a tob and also a
path from b to a. The strongly connected components of a directed graphG are its maximal
strongly connected sub graphs. If each strongly connected component is contracted to a single
vertex, the resulting graph is a directed acyclic graph. In other words, two vertices of directed
graph are in the same component if and only if they are reachable from each other.

Figure 11.32 – Strong Components

The above directed graph has 4 strongly connected components: C1, C2, C3 and C4. If G has an
edge from some vertex in Ci to some vertex in Cj where i ≠ j, then one can reach any vertex in Cj
from any vertex in Ci but not return. In the example, one can reach any vertex in C 2 from any
vertex in C1 but cannot return to C1 from C2.

224
Chapter 11- Graphs

Algorithm

A DFS(G) produces a forest of DFS-trees. Let C be any strongly connected component of G, let
v be the first vertex on C discovered by the DFS and let T be the DFS-tree containing v when
DFS-visit(v) is called all vertices in C are reachable from v along paths containing visible
vertices; DFS-visit(v) will visit every vertex in C, add it to T as a descendant of v.

Strongly-connected-components (G)

1. Call DFS(G) to compute finishing times f[u] for all u.


2. Compute GT
3. Call DFS(GT), but in the main loop, consider vertices in order of decreasing f[u] (as
computed in first DFS)
4. Output the vertices in each tree of the depth-first forest formed in second DFS as a
separate SCC.

11.8 Summary

In this chapter we have seen how graphs can be used to model many real-life problems. Many of
thegraphs that occur are typically very sparse, so it is important to pay attention to the
datastructures that are used to implement them.

11.9 Key Terms

Graph, adjacency matrix, adjacency list, Topological sort, dijkstra’s algorithm, prim’s algorithm,
Kruskal’s algorithm, biconnectivity, articulation points, Depth first search, breadth first search
and strong component.

11.10 Review Questions ?????????????

Two mark Questions


1. Define graph.
2. What are the applications of Graph?
3. Define loop.
4. What are the various representations of graph?
5. What do you mean by connected graph?
6. Define strongly connected graph.
7. Define weekly connected graph.

225
Chapter 11- Graphs

8. Define directed graph, weighted graph and unweighted graph.


9. What are the types of graph traversals?
10. Define DFS.
11. Define BFS.
12. What are the applications of BFS?
13. What is the difference between DFS and BFS?
14. What are the merits and demerits of DFS?
15. What are the merits and demerits of BFS?
16. Define topological sort.
17. Define single source shortest path algorithm.
18. Define spanning tree
19. Define minimum spanning tree.
20. What are the algorithms are used to find minimum spanning tree?
21. Define prim’s algorithm.
22. Define kruskal’s algorithm.
23. Compare Prim’s algorithm and Kruskal’s algorithm.
24. What are the applications of DFS?
25. What do you mean by biconnectivity?
26. What do you mean by articulation points?
27. Define Euler circuits.
28. How to find strong components in the graph?
29. Define depth first spanning forest.

Big Questions
1.Explainthevarious representationofgraphwithexamplein detail?
2.Define topological sort? Find a topological ordering for the following graph.

3. Explain Dijkstra's algorithm and find the shortest path from A to all other vertices for the
following graph.

226
Chapter 11- Graphs

4.ExplainPrim's algorithmand find the minimum spanning tree for the graph.

5. Explain Kruskal's algorithm and find the minimum spanning tree for the above graph.
6. Find all the articulation points for the following graph.

227

Potrebbero piacerti anche