You are on page 1of 14

Graphs

CHAPTER 6 LEC 2
Outline
Cycle Detection
Spanning Trees
Topological Sort
Cycle Detection
Can be implemented with small modifications in DFS(v)

cycleDetectionDFS(v)
num(v) = i++;
for all vertices u adjacent to v
if num(u) is 0
attach edge(uv) to edges;
cycleDetectionDFS(u);
else if edge(vu)is notin edges
cycle detected;
Cycle Detection- Application
 Deadlock detection in
operation system

 check for errors in code


Spanning Tree
Is a tree formed from graph edges that connects all the
vertices of G at lowest total cost.
Application
-House installation
-Transportation path
Spanning Tree - Kruskal’s
Algorithm
KruskalAlgorithm(weighted connected undirected graph)
tree = null;
edges = sequence of all edges of graph sorted by weight;
for (i = 1; i ≤ |E| and |tree| < |V| – 1; i++)
if ei from edges does not form a cycle with edges in tree
add ei to tree;
example
Spanning Tree – Dijkstra
Method
Almost similar with shortest path
Difference (update rule)
 After a vertex v is selected, for each unknown w adjacent
to v, dw= min(dw,cw,v)
Initial configuration After v1 After V4
Example
after v2, v3 are declared after v7 is declared

After v6 and v5are selected (final)


The edges in the spanning tree can be
read from the table:
(v2,v1), (v3,v4), (v4,v1), (v5,v7),
(v6,v7), (v7,v4)
Topological Sort
 The dependencies between tasks can be shown in the form
of a digraph.
 A topological sort linearizes a digraph

topologicalSort(digraph)
for i = 1 to |V|
find a minimal vertex v;
num(v) = i;
remove from digraph vertex v and all edges incidentwith v;
Topological Sort -example
Topological Sort - DFS
TS(v)
num(v) = i++;
for all vertices u adjacent to v
if num(u) == 0
TS(u);
else if TSNum(u) == 0
error;
TSNum(v) = j++;
Topological Sort - example
Assignment
Select any application that can be modeled with common
problems of graph such as path, shortest path, minimum
spanning tree, cycle detection, topological sort, graph
coloring, networking and matching.
Model and implement the application and report a word
document which contain statement of the problem, model of
the problem and implementation.
Refer Adam Drozdek 8.13 Case Study: Distinct
Representatives (p 450) as a sample
Due date January 13, 2014. e-mail my2alew@gmail.com

You might also like