A,C

CSCE 210
Data Structures and Algorithms
Prof. Amr Goneid
AUC
Part 10. Graphs
Prof. Amr Goneid, AUC
1
Graphs
Prof. Amr Goneid, AUC
2
Graphs
 Basic Definitions
 Paths and Cycles
 Connectivity
 Other Properties
 Representation
 Examples of Graph Algorithms:
 Graph Traversal
 Shortest Paths
 Minimum Cost Spanning Trees
Prof. Amr Goneid, AUC
3
1. Basic Definitions
A graph G (V,E) can be defined
A
F
as a pair (V,E) , where V is a set
B
G
E
of vertices, and E is a set of
edges between the vertices
C
D
E = {(u,v) | u, v  V}. e.g.
V = {A,B,C,D,E,F,G}
E = {( A,B),(A,F),(B,C),(C,G),(D,E),(D,G),(E,F),(F,G)}
If no weights are associated with the edges, an edge is
either present(“1”) or absent (“0”).
Prof. Amr Goneid, AUC
4
Basic Definitions
 A graph is like a road map. Cities are
vertices. Roads from city to city are edges.
 You could consider junctions to be vertices,
too. If you don't want to count them as
vertices, a road may connect more than two
cities. So strictly speaking you have
hyperedges in a hypergraph.
 If you want to allow more than one road
between each pair of cities, you have a
multigraph, instead.
Prof. Amr Goneid, AUC
5
Basic Definitions
Adjacency: If vertices u,v have
3
A
F
2
an edge e = (u,v) | u, v  V then 2
1
u and v are adjacent.
B
G
E
A weighted graph has a weight
5 4
2
1
associated with each edge.
C
D
Undirected Graph is a graph in which the adjacency is
symmetric, i.e., e = (u,v) = (v,u)
A Sub-Graph: has a subset of the vertices and the
edges
Prof. Amr Goneid, AUC
6
Basic Definitions
Directed Graph: is a graph in which
2
adjacency is not symmetric,
B
i.e., (u,v)  (v,u)
Such graphs are also called
1
“Digraphs”
3
A
F
2
1
G
E
5 4
C
2
D
Directed Weighted Graph:
A directed graph with a weight
for each edge. Also called a network.
Prof. Amr Goneid, AUC
7
2. Paths & Cycles
B
G
C
F
A
F
A
E
B
D
G
C
E
D
Path: A list of vertices of a graph where each vertex
has an edge from it to the next vertex.
Simple Path: A path that repeats no vertex.
Cycle: A path that starts and ends at the same vertex
and includes other vertices at most once.
Prof. Amr Goneid, AUC
8
Directed Acyclic Graph (DAG)
F
A
B
G
C
E
D
Directed Acyclic Graph (DAG): A directed graph with
no path that starts and ends at the same vertex
Prof. Amr Goneid, AUC
9
Hamiltonian Cycle
F
A
B
G
C
E
D
Icosian Game
Hamiltonian Cycle:
A cycle that includes all other vertices only once,
e.g. {D,B,C,G,A,F,E,D}
Named after Sir William Rowan Hamilton (1805 –1865)
The Knight’s Tour problem is a Hamiltonian cycle problem
Prof. Amr Goneid, AUC
10
Hamiltonian Cycle Demo
 A Hamiltonian Cycle is a cycle that visits each node exactly
once. Here we show a Hamiltonian cycle on a 5-dimensional
hypercube. It starts by completely traversing the 4-dimensional
hypercube on the left before reversing the traversal on the right
subcube.
 Hamiltonian cycles on hypercubes provide constructions for
Gray codes: orderings of all subsets of n items such that
neighboring subsets differ in exactly one element.
 Hamilitonian cycle is an NP-complete problem, so no worst-case
efficient algorithm exists to find such a cycle.
 In practice, we can find Hamiltonian cycles in modest-sized
graphs by using backtracking with clever pruning to reduce the
search space.
Prof. Amr Goneid, AUC
11
Hamiltonian Cycle Demo
http://www.cs.sunysb.edu/~skiena/comb
inatorica/animations/ham.html
Hamiltonian Cycle Demo
Prof. Amr Goneid, AUC
12
Euler Circuit
Leonhard Euler
(1736)
Konigsberg Bridges
(not Eulerian)
Euler Circuit:
A cycle that includes every edge once.
Used in bioinformatics to reconstruct the DNA sequence
from its fragments
Prof. Amr Goneid, AUC
13
Euler Circuit Demo
 An Euler circuit in a graph is a
traversal of all the edges of the
graph that visits each edge
exactly once before returning
home. A graph has an Euler circuit
if and only if all its vertices are that
of even degrees.
 It is amusing to watch as the Euler circuit finds a way
back home to a seemingly blocked off start vertex.
We are allowed (indeed required) to visit vertices
multiple times in an Eulerian cycle, but not edges.
Prof. Amr Goneid, AUC
14
Euler Circuit Demo
http://www.cs.sunysb.edu/~skiena/combin
atorica/animations/euler.html
Euler Circuit Demo
Prof. Amr Goneid, AUC
15
3. Connectivity
A
F
B
G
C
A
E
F
B
D
G
C
E
D
Connected Graph: An undirected graph with a path
from every vertex to every other vertex
A Disconnected Graph may have several connected
components
Tree: A connected Acyclic graph
Prof. Amr Goneid, AUC
16
Connected Components Demo
 What happens when you start with an empty
graph and add random edges between
vertices?
 As you add more and more edges, the
number of connected components in the
graph can be expected to drop, until finally
the graph is connected.
 An important result from the theory of random
graphs states that such graphs very quickly
develop a single ``giant'' component which
eventually absorbs all the vertices.
Prof. Amr Goneid, AUC
17
Connected Components Demo
http://www.cs.sunysb.edu/~skiena/combin
atorica/animations/concomp.html
Randomly Connected Graph Demo
Prof. Amr Goneid, AUC
18
Connectivity
A
F
B
G
C
A
E
F
B
D
E
C
D
Articulation Vertex: if removed with all of its edges will
cause a connected graph to be disconnected, e.g., G
and D are articulation vertices
Prof. Amr Goneid, AUC
19
Connectivity
Degree Of a vertex,
F
A
the number of edges connected to
it.
B
G
E
Degree Of a graph,
C
D
the maximum degree of any vertex
(e.g. B has degree 2, graph has degree 3).
In a connected graph the sum of the degrees is twice
the number of edges, i.e
V
d
i 1
i
 2E
Prof. Amr Goneid, AUC
20
Connectivity
F
A
B
G
C
E
D
In-Degree/Out-Degree: the number of edges coming
into/emerging from a vertex in a connected graph (e.g.
G has in-degree 3 and out-degree 1).
Prof. Amr Goneid, AUC
21
Connectivity
Complete Graph:
A
There is an edge between
every vertex and every
B
C
other vertex. In this case,
the number of edges is
maximum:
V (V  1 )
Emax 
2
Notice that the minimum number of edges for a
connected graph ( a tree in this case) is (V-1)
Prof. Amr Goneid, AUC
D
22
Density (of edges)
Density of a Graph: D  2 E
V (V  1)
Dense Graph:
Number of edges is close to Emax = V(V-1)/2.
So, E = O(V2) and D is close to 1
Sparse Graph:
Number of edges is close to Emin = (V-1).
So, E = O(V)
Prof. Amr Goneid, AUC
23
4. Other Properties
D
A
B
C
D
A
B
C
Planar Graph:
A graph that can be drawn in
the plain without edges
crossing
Non-Planar
Prof. Amr Goneid, AUC
24
Other Properties
Graph Coloring:
To assign color (or any distinctive mark) to vertices
such that no two adjacent vertices have the same color.
The minimum number of colors needed is called the
Chromatic Order of the graph (G).
For a complete graph, (G) = V.
Prof. Amr Goneid, AUC
1
2
4
3
25
Other Properties
An Application:
Find the number of exam slots for 5 courses. If a single
student attends two courses, an edge exists between them.
EE
CS
Econ
Math
Phys
Slot
Courses
1 (red)
CS
2 (Green)
EE, Econ,
Phys
3 (Blue)
Math
Prof. Amr Goneid, AUC
26
5. Representation
Adjacency Matrix:
 V x V Matrix a(i,j)
 a(i,j) = 1 if vertices (i) and (j) are adjacent, zero otherwise.
Usually self loops are not allowed so that a(i,i) = 0.
 For undirected graphs, a(i,j) = a(j,i)
A B C D
 Matrix is symmetric
A
B
D
C
Prof. Amr Goneid, AUC
A
B
C
D
0
1
1
0
1
0
1
0
1
1
0
1
0
0
1
0
27
Adjacency Matrix
 For weighted undirected graphs, a(i,j) = a(j,i) = wij
D
A
1
2
B
5
C
3
Prof. Amr Goneid, AUC
A
B
C
D
A
0
2
1
0
B
2
0
3
0
C
1
3
0
5
D
0
0
5
0
28
Adjacency Matrix
 For weighted directed graphs, a(i,j) ≠ a(j,i)
 Matrix is not symmetric
D
A
2
1
B
5
C
3
Prof. Amr Goneid, AUC
A
B
C
D
A
0
0
0
0
B
2
0
3
0
C
1
0
0
0
D
0
0
5
0
29
Representation
The adjacency matrix is appropriate for dense graphs
but not compact for sparse graphs.
e.g., for a lattice graph, the degree
of a vertex is 4, so that E = 4V.
Number of “1’s” to total matrix size
is approximately 2 E / V2 = 8 / V.
For V >> 8, the matrix is dominated by zeros.
Prof. Amr Goneid, AUC
30
Representation
Adjacency List:
An array of vertices with pointers
to linked lists of adjacent nodes, e.g.,
A
B
B
A
C
A
D
C
C
A
B
D
C
C
B
D
The size is O(E + V) so it is compact for sparse graphs.
Prof. Amr Goneid, AUC
31
Edge List
 An edge (u,v,w) existing between nodes (u) and (v) with weight
(w) is modeled as a structure (Edge).
A
A
A
B
C
v
B
C
C
D
w
2
1
3
5
D
1
2
B
u
3
5
C
 Space requirements is O(E)
Prof. Amr Goneid, AUC
32
6. Examples of Graph Algorithms
 Examples of Graph Algorithms:
 Graph
Traversal
 Shortest Paths
 Minimum Cost Spanning Trees
Prof. Amr Goneid, AUC
33
6.1 Graph Traversal
Depth-First Search (DFS)
 Visits every node in the graph by
following node connections in depth.
 Recursive algorithm.
 An Array val[v] records the order in
which vertices are visited. Initialized to
“unseen”.
 Any edge to a vertex that has not been
seen is followed via the recursive call.
Prof. Amr Goneid, AUC
34
Algorithm
// Assume order = 0 initially
void DFS()
{
int k; int unseen = -2;
// Initialize all to unseen
for (k = 1; k <= v; k++) val[k] = unseen;
// Follow Nodes in Depth
for (k = 1; k <= v; k++)
if (val[k] == unseen) visit(k);
}
Prof. Amr Goneid, AUC
35
Algorithm (continued)
void visit(int k)
{ int t;
val[k] = ++order;
for (t = 1; t <= v; t++)
if (a[k][t] != 0)
if (val[t] == unseen) visit(t);
}
Prof. Amr Goneid, AUC
36
Example
1
A
start
A
B
C
D
E
F
G
A
0
1
1
0
0
1
1
B
1
0
0
0
0
0
0
C
1
0
0
0
0
0
0
VAL[K]
D
0
0
0
0
1
1
0
E
0
0
0
1
0
1
1
F
1
0
0
1
1
0
0
G
1
0
0
0
1
0
0
B
C
D
F
G
E
A B C D E F G
1
Prof. Amr Goneid, AUC
37
Example
1
A
A
B
C
D
E
F
G
A
0
1
1
0
0
1
1
B
1
0
0
0
0
0
0
C
1
0
0
0
0
0
0
VAL[K]
D
0
0
0
0
1
1
0
E
0
0
0
1
0
1
1
F
1
0
0
1
1
0
0
G
1
0
0
0
1
0
0
2 B
C
D
F
G
E
A B C D E F G
1 2
Prof. Amr Goneid, AUC
38
Example
1
A
A
B
C
D
E
F
G
A
0
1
1
0
0
1
1
B
1
0
0
0
0
0
0
C
1
0
0
0
0
0
0
VAL[K]
D
0
0
0
0
1
1
0
E
0
0
0
1
0
1
1
F
1
0
0
1
1
0
0
G
1
0
0
0
1
0
0
2 B
3
C
D
F
G
E
A B C D E F G
1 2 3
Prof. Amr Goneid, AUC
39
Example
1
A
A
B
C
D
E
F
G
A
0
1
1
0
0
1
1
B
1
0
0
0
0
0
0
C
1
0
0
0
0
0
0
VAL[K]
D
0
0
0
0
1
1
0
E
0
0
0
1
0
1
1
F
1
0
0
1
1
0
0
G
1
0
0
0
1
0
0
2 B
3
C
D
4 F
G
E
A B C D E F G
1 2 3
4
Prof. Amr Goneid, AUC
40
Example
1
A
A
B
C
D
E
F
G
A
0
1
1
0
0
1
1
B
1
0
0
0
0
0
0
C
1
0
0
0
0
0
0
VAL[K]
D
0
0
0
0
1
1
0
E
0
0
0
1
0
1
1
F
1
0
0
1
1
0
0
G
1
0
0
0
1
0
0
2 B
3
5
C
D
4 F
G
E
A B C D E F G
1 2 3 5
4
Prof. Amr Goneid, AUC
41
Example
1
A
A
B
C
D
E
F
G
A
0
1
1
0
0
1
1
B
1
0
0
0
0
0
0
C
1
0
0
0
0
0
0
VAL[K]
D
0
0
0
0
1
1
0
E
0
0
0
1
0
1
1
F
1
0
0
1
1
0
0
G
1
0
0
0
1
0
0
2 B
3
5
C
D
4 F
G
E
6
A B C D E F G
1 2 3 5 6 4
Prof. Amr Goneid, AUC
42
Example
1
A
A
B
C
D
E
F
G
A
0
1
1
0
0
1
1
B
1
0
0
0
0
0
0
C
1
0
0
0
0
0
0
VAL[K]
D
0
0
0
0
1
1
0
E
0
0
0
1
0
1
1
F
1
0
0
1
1
0
0
G
1
0
0
0
1
0
0
7
2 B
3
5
C
D
4 F
G
E
6
A B C D E F G
1 2 3 5 6 4 7
Prof. Amr Goneid, AUC
43
Exercises
 Show how DFS can be used to
determine the number of connected
components in a graph.
 Explore the non-recursive version of the
DFS using a stack. What will happen if
you use a queue instead of the stack?
Prof. Amr Goneid, AUC
44
Non-Recursive DFS
// Assume unseen = -2 and hold = -1
// Val[ ] is set to “unseen” initially , order = 0
Stact<int> S; // A stack of integers
void DFS(int k)
{
int t;
S.push(k);
while (! S.stackIsEmpty())
{
S.pop(k); val[k] = ++order;
for (t = v; t >= 1; t--) // Scan from right to left
if (a[k][t] != 0) if (val[t] == unseen)
{ S.push(t); val[t] = hold;}
}
}
Prof. Amr Goneid, AUC
45
Breadth-First Search (BFS)
// Replacing the stack by a queue, gives the BFS algorithm
Queuet<int> Q; // A queue of integers
void BFS(int k)
{
int t;
Q.enqueue(k);
while (! Q.queueIsEmpty())
{
Q.dequeue(k); val[k] = ++order;
for (t = 1; t <= v; t++) // Scan from left to right
if (a[k][t] != 0) if (val[t] == unseen)
{ Q.enqueue(t); val[t] = hold;}
}
}
Prof. Amr Goneid, AUC
46
Example BFS
1
A
2 B
3
6
C
D
4 F
5 G
E
7
A B C D E F G
1 5 3 6 7 4 5
Prof. Amr Goneid, AUC
47
DFS and BFS of a Tree
For a tree structure:
 DFS is equivalent to Pre-order
traversal
 BFS is equivalent to Level-order
traversal
DFS Demo
BFS Demo
Prof. Amr Goneid, AUC
48
Graph Traversal Demos
http://www.cs.sunysb.edu/~skiena/combi
natorica/animations/search.html
http://www.cosc.canterbury.ac.nz/people/
mukundan/dsal/GraphAppl.html
Prof. Amr Goneid, AUC
49
Exercise
 Model the shown maze
in
as a graph.
 Show how DFS can be
used to find the exit.
Prof. Amr Goneid, AUC
out
50
8. A Simple Graph Class
 To represent a weighted undirected graph with a maximum of
Vmax vertices and Emax = Vmax(Vmax-1)/2 edges. The verices are
numbered 0,1,...V-1.
 The graph is assumed to be on a text file in the form of an
adjacency matrix. The weights on the edges are assumed to be
positive integers with zero weight indicating the absence of an
edge.
 When loaded from the text file, the weights are stored in a 2-D
array (AdjMatrix) representing the adjacency matrix. Another
array (edges) stores the non-zero edges in the graph.
 An edge (u,v,w) existing between nodes (u) and (v) with weight
(w) is modeled as a class (Edge).
Prof. Amr Goneid, AUC
51
Edge Class
// File: Edge.h
// Definition of Edge class
#ifndef EDGE_H
#define EDGE_H
typedef int weightType;
// weights are positive integers
class Edge
{
public:
int u,v; weightType w;
bool operator < (const Edge &e) { return (w < e.w); }
bool operator <= (const Edge &e) { return (w <= e.w); }
}; // end of class Edge declaration
#endif // EDGE_H
Prof. Amr Goneid, AUC
52
Graph Class
// File: Graphs.h
// Graph library header file
#ifndef GRAPHS_H
#define GRAPHS_H
#include <string>
#include "Edge.h"
using namespace std;
const int Vmax = 50;
// Maximum number of vertices
const int Emax = Vmax*(Vmax-1)/2; // Maximum number of edges
Prof. Amr Goneid, AUC
53
Graph Class
class Graphs
{
public:
Graphs();
// Constructor
~Graphs(); // Destructor
// Map vertex number to a name (character)
char Vname(const int s) const;
void getGraph(string fname); // Get Graph from text File (fname)
void dispGraph( ) const;
// Display Ajacency Matrix
int No_of_Verices( ) const;
// Get number of vertices (V)
int No_of_Edges( ) const;
// Get Number of Non-zero edges (E)
void dispEdges( ) const;
// Display Graph edges
void DFS( );
// Depth First Search Traversal (DFS)
Prof. Amr Goneid, AUC
54
Graph Class
private:
int V, E;
// No.of vertices (V) and edges (E)
weightType AdjMatrix[Vmax][Vmax]; // Adjacency Matrix
Edge edges[Emax];
// Array of non-zero edges
int order;
// Order of Visit of a node in the DFS
int val[Vmax];
// Array holding order of traversal
void getEdges();
// Get edges from adjacency matrix
void printEdge(Edge e) const; // Output an edge (e)
void visit(int k);
// Node Visit Function for DFS
};
#endif // GRAPHS_H
#include "Graphs.cpp"
Prof. Amr Goneid, AUC
55
6.2 Shortest Paths
(General)
 In a graph G(V,E), find shortest paths from a
single source vertex (S) to all other vertices.
 Edsger Dijkstra published an algorithm to
solve this problem in 1959.
Prof. Amr Goneid, AUC
56
Shortest Paths:
Dijkstra’s Algorithm
 Dijkstra’s Algorithm for V vertices:
Uses three arrays:
- Distance[i]: holds distance from (S) to
vertex (i).
- Processed[i]: to flag processed vertices.
- Via[i]: holds index of vertex from which we
can directly reach vertex (i).
Prof. Amr Goneid, AUC
57
Initialization
 0 if i  s

Dis tan ce [ i ]   w si if ( s,i )  E
  if ( s,i )  E

 yes if i  s
processed [ i ]  
 no if i  s
 s if ( s,i )  E
Via [ i ]  
 0 if ( s,i )  E
Prof. Amr Goneid, AUC
58
Method
closest to S
not yet processed
Distance[j]
j
Already Processed
Wij
S
z
y
x
Distance[i]
i
adjacent to j
Prof. Amr Goneid, AUC
59
Dijkstra’s Algorithm
Repeat
 Find j = index of unprocessed node closest to (S)
 Mark (j) as now processed
 For each node (i) not yet processed:
if (i) is adjacent to (j) then {
new_distance = Distance[j] + Wij
if new_distance < Distance[i] then
{ Distance[i] = new_distance ;
Via[i] = j ; }
}
Until all vertices are processed
Prof. Amr Goneid, AUC
60
Example
Initial Source = A
D
E
0 15 35 
20
Dist
Processed
A
B
yes No
0
A
C
No
No
No
A
0
A
15
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
61
Example
j=B
D
E
0 15 35 
20
Dist
No
No
Processed
0
A
A
B
C
yes yes No
0
A
A
15
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
62
Example
j=B
A
i=D
B
C
D
E
0 15 35 55 20
yes yes No
0
A
A
No
No
B
A
Dist
15
A
20
B
Processed
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
63
Example
j=E
A
B
C
D
E
0 15 35 55 20
yes yes No
0
A
A
No
B
Dist
15
yes Processed
A
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
64
Example
j=E
A
i=C
B
C
D
E
0 15 30 55 20
yes yes No
0
A
E
No
B
Dist
15
yes Processed
A
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
65
Example
j=C
A
B
C
D
E
0 15 30 55 20
yes yes yes No
0
A
E
B
Dist
15
yes Processed
A
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
66
Example
j=C
A
i=D
B
C
D
E
0 15 30 55 20
yes yes yes No
0
A
E
B
Dist
15
yes Processed
A
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
67
Example
j=D
A
B
C
D
E
0 15 30 55 20
Dist
15
yes yes yes yes yes Processed
0
A
E
B
A
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
68
Example
Final
A
B
C
D
E
0 15 30 55 20
Dist
15
yes yes yes yes yes Processed
0
A
A→B
A→E→C
A→B→D
A→E
E
B
A
A
20
B
E
35
Via
40
D
Prof. Amr Goneid, AUC
10
35
C
69
How to print the path?
/* The function Vname(k) maps a vertex number to a name (e.g
a character A, B,.. etc). Given the via[ ] array resulting from
Dijkstra’s algorithm, the following recursive function prints
the vertices on the shortest path from source (s) to
destination (i).
*/
void Graphs::printPath(int s, int i) const
{
if (i == s) cout << Vname(s);
else
{
printPath(s,via[i]); cout << Vname(i); }
}
Prof. Amr Goneid, AUC
70
Demo
http://www.cs.sunysb.edu/~skiena/com
binatorica/animations/dijkstra.html
Shortest Paths Demo1
Shortest Paths Demo2
Prof. Amr Goneid, AUC
71
6.3 Minimum Cost Spanning Trees
(a) Spanning Tree
Consider a connected undirected graph
G(V,E). A sub-graph S(V,T) is a
spanning tree of the graph (G) if:
 V(S) = V(G) and T  E
 S is a tree, i.e., S is connected and
has no cycles
Prof. Amr Goneid, AUC
72
Spanning Tree
E
F
A
G
B
D
C
S(V,T):
V = {A,B,C,D,E,F,G}
T = {AB,AF,CD,DE,EF,FG}
Prof. Amr Goneid, AUC
73
Spanning Tree
E
F
A
G
D
Notice that:
B
C
|T| = |V| - 1 and adding any edge (u,v) T will
produce a cycle so that S is no longer a spanning
tree (e.g. adding (G,D))
Prof. Amr Goneid, AUC
74
One Graph, Several Spanning
Trees
Prof. Amr Goneid, AUC
75
Spanning Forest
E
F
A
G
B
D
C
For a connected undirected graph G(V,E), a spanning forest
is S(V,T) if S has no cycles and TE. S(V,T) will be
composed of trees (V1,T1), (V2,T2), …, (Vk,Tk), k ≤ |V|
Prof. Amr Goneid, AUC
76
(b) Minimum Cost Spanning Tree (MST)
Consider houses A..F connected by
muddy roads with the distances
4
indicated. We want to pave some
E
A
roads such that:
7
3 3
 We can reach a house from
any other house via paved roads.
C
B
 The cost of paving is minimum.
This problem is an example of
6
4
finding a Minimum Spanning Tree
D
(MST)
Prof. Amr Goneid, AUC
4
2
G
9
F
5
77
Minimum Spanning Tree (MST)
 Cost:
For a weighted graph,
4
E
A
the cost of a spanning tree
7
is the sum of the weights
3 3
of the edges in that tree.
C
B
 Minimum Spanning tree:
4
A spanning tree of minimum cost 6
 For the shown graph, the
D
minimum cost is 22
Prof. Amr Goneid, AUC
4
2
G
9
F
5
78
Kruskal’s Algorithm for MST
 The algorithm was written by
Joseph Kruskal in 1956
 A Greedy Algorithm:
Builds the MST edge by edge into a set of edges (T). At a given
stage, chooses an edge that results in minimum increase in the
sum of costs included so far in (T).
The set (T) might not be a tree at all stages of the algorithm, but
it can be completed into a tree iff there are no cycles in (T).
Prof. Amr Goneid, AUC
79
Kruskal’s Algorithm for MST
 Builds up forests , then joins them in a
single tree.
 Constraints:
- The graph must be connected.
- Uses exactly V-1 edges.
- Excludes edges that form a cycle.
Prof. Amr Goneid, AUC
80
Abstract Algorithm
-Form Set E of edges in increasing order of costs.
- Set MST T = empty
-Repeat
 Select an edge (e) from top.
 Delete edge from E set.
 Add edge (e) to (T) if it does not form a cycle, otherwise,
reject.
-Until we have V-1 successful edges.
Prof. Amr Goneid, AUC
81
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w
2
3
3
4
4
4
5
6
accept
4
E
A
7
3
2
C
B
Prof. Amr Goneid, AUC
3
4
6
4
G
9
F
5
D
82
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w accept
2
yes
3
3
4
4
4
5
6
4
E
A
7
3
2
C
B
Prof. Amr Goneid, AUC
3
4
6
4
G
9
F
5
D
83
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w accept
2
yes
3
yes
3
4
4
4
5
6
4
E
A
7
3
2
C
B
Prof. Amr Goneid, AUC
3
4
6
4
G
9
F
5
D
84
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w accept
2
yes
3
yes
3
yes
4
4
4
5
6
4
E
A
7
3
2
C
B
Prof. Amr Goneid, AUC
3
4
6
4
G
9
F
5
D
85
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w
2
3
3
4
4
4
5
6
accept
yes
yes
yes
4
E
A
7
3
NO
2
C
B
Prof. Amr Goneid, AUC
3
4
6
4
G
9
F
5
D
86
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w
2
3
3
4
4
4
5
6
accept
yes
yes
yes
4
7
3
NO
yes
E
A
2
C
B
Prof. Amr Goneid, AUC
3
4
6
4
G
9
F
5
D
87
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w
2
3
3
4
4
4
5
6
accept
yes
yes
yes
4
7
3
NO
yes
yes
E
A
2
C
B
Prof. Amr Goneid, AUC
3
4
6
4
G
9
F
5
D
88
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w
2
3
3
4
4
4
5
6
accept
yes
yes
yes
4
7
3
NO
yes
yes
E
A
Prof. Amr Goneid, AUC
2
C
B
NO
3
4
6
4
G
9
F
5
D
89
Example
Edge
1
2
3
4
5
6
7
8
u
E
A
C
A
C
E
D
B
v
F
C
E
E
D
G
F
D
w
2
3
3
4
4
4
5
6
accept
yes
yes
yes
4
7
3
NO
yes
yes
E
A
2
C
B
NO
3
4
6
4
G
9
F
5
D
yes
Prof. Amr Goneid, AUC
90
How to test for Cycles?
Simple Union- Simple Find
Given a set (1,2,..,n} initially partitioned into n
disjoint sets (each element is its own parent):
 Find (i): return the sub-set that (i) is in (i.e.
return the parent set of i).
 Union (k,j): combine the two sub-sets that (j)
and (k) are in (make k the child of j)
 Union builds up a tree, while find will search
for the root of the tree.
 Cost is O(log n)
Prof. Amr Goneid, AUC
91
How to test for Cycles?
 Represent vertices as Disjoint Sets
A
B
C
D
E
F
G
 Initially, each node is its own parent (-1)
A B C D E F G
1 2 3 4 5 6 7
-1 -1 -1 -1 -1 -1 -1
Prof. Amr Goneid, AUC
Let the parent set
of u be p(u)
92
How to test for Cycles?
 When edge (E,F) is selected, we find that p(E)  p(F). So,
we make a union between p(E) and p(F), i.e., p(F) becomes
the child of p(E). Same for edge (A,C)
A
B
C
D
A B C D E F
1 2 3 4 5 6
-1 -1 1 -1 -1 5
E
G
7
-1
Prof. Amr Goneid, AUC
F
G
Parent table after
selecting (E,F)
then (A,C)
93
How to test for Cycles?
When (C,E) is selected, we find that P(C)  P(E). This means that
the edge will be accepted.
 Select (A,E), Find will give P(A) = P(E) (cycle, reject).

D
A
B
E
C
A B C D E F
1 2 3 4 5 6
-1 -1 1 -1 3 5
G
7
-1
Prof. Amr Goneid, AUC
F
G
Parent table after
accepting (E,F) then
(A,C), then (C,E),
then rejecting (A,E)
94
How to test for Cycles?
When (C,D) is selected, we find that P(C)  P(D). This means that
the edge will be accepted.
 Select (E,G), Find will give P(E)  P(G) (accept).

G
D
A
B
E
C
A B C D E F
1 2 3 4 5 6
-1 -1 1 3 3 5
G
7
5
F
Parent table after 5th
accepted edge
Prof. Amr Goneid, AUC
95
How to test for Cycles?

When (D,F) is selected, we find that P(D) = P(F). This means that
the edge will be rejected.
 Select (B,D), Find will give P(B)  P(D) (accept).
G
D
A
B
E
C
A B C D E F
1 2 3 4 5 6
-1 4 1 3 3 5
G
7
5
F
Parent table after 6th
accepted edge
(Final MST)
Prof. Amr Goneid, AUC
96
Kruskal’s Algorithm
Insert edges with weights into a minimum heap
Put each vertex in a separate set
i=0;
While ((i < V-1) && (heap not empty)) {
Remove edge (u,v) from heap
Find set (j) of connected vertices having (u)
Find set (k) of connected vertices having (v)
if ( j != k ) {
i++; MST [i].u = u; MST [i].v = v;
MST [i].w = w;
9.
Make a Union between set (j) and set (k); }
}
1.
2.
3.
4.
5.
6.
7.
8.
Prof. Amr Goneid, AUC
97
Kruskal’s Algorithm Demo
http://www.cs.sunysb.edu/~skiena/combinatorica/
animations/mst.html
Kruskal's algorithm at work on a graph of distances
between 128 North American cities. Almost imperceptively
at first, short edges get added all around the continent,
slowly building forests until the tree is completed.
MST (Kruskal) Demo1
MST (Kruskal) Demo2
Prof. Amr Goneid, AUC
98
Learn on your own about:
 Directed Graphs or Digraphs
 Edge-List representation of graphs
 Prim’s algorithm for Minimum Spanning Trees. The
algorithm was developed in 1930 by Czech mathematician
Vojtěch Jarník
and later independently by
computer scientist
Robert C. Prim in 1957
and rediscovered by
Edsger Dijkstra in 1959. Therefore it is also sometimes
called the DJP algorithm, the Jarník algorithm, or the Prim–
Jarník algorithm.
Prof. Amr Goneid, AUC
99