© 2015 JW Ryder CSCI 203 Data Structures 1 K oenigsberg Bridge Problem K neiphof Island A B C D a b c d e PregalRiver Leonhard Euler-1736
© 2015 JW Ryder
CSCI 203 Data Structures 1
Koenigsberg Bridge Problem
Kneiphof Island
A
B
C
D
a b
c d
e
Pregal River
Leonhard Euler - 1736
© 2015 JW Ryder
CSCI 203 Data Structures 2
Graphs and Graph Algorithms
Eulerian c ircuit There is a walk, beginning at any vertex v,
going through each edge exactly once, andterminating at the starting vertex (v) iff thedegree of each vertex is even.
Any walk satisfying this criteria is anEulerian c ircuit (cycle, walk)
© 2015 JW Ryder
CSCI 203 Data Structures 3
Graph definitions
Graph: A graph G, consists of 2 sets, a finite non-empty set of vertices, and a finite possibly emptyset of edges. V(G) and E(G) represent the sets ofvertices and edges of G. [ G = (V, E) ]
Undirected graph: The pair of verticesrepresenting any edge is unordered. (v0, v1) = (v1,v0)
Directed graph: Each edge is represented as adirected pair of vertices. (v0, v1) != (v1, v0)
Tail/Head: Arrow head, not arrow tail
© 2015 JW Ryder
CSCI 203 Data Structures 4
3 Sample Graphs
0
1 2
3
G1
63
0
21
4 5
G2
G3
0
1
2
© 2015 JW Ryder
CSCI 203 Data Structures 5
Two Very Important Rules
A graph may not have an edge from a vertex iback to itself (no self loops)
A graph may not have multiple occurrences ofthe same edge
1. Complete graph: is a graph that has themaximum number of edges2. For undirected graph with n vertices
3. max edges = n (n - 1) / 24. For directed graph with n vertices
5. max edges = n (n - 1)6. Show example with 4 vertex complete graph
v0 and v1 are adjacent in an undirected graph ifthe edge (v0, v1) exists G2 - 3, 4, 0 are adjacent to 0 G2 - edges (0, 2), (2, 5), (2, 6) are inc ident on
v2
<v0, v1> is a directed edge, then v0 is adjacentto v1 and v1 is adjacent from v0. Edge <v0, v1> isinc ident on v0 and v1.
Subgraph of G: is a graph G' such that V(G') isa proper subset of V(G) and E(G') is a propersubset of E(G)
© 2015 JW Ryder
CSCI 203 Data Structures 6
More Definitions
Path: show path from vp to vq in a graph G - vp,vi1, vi2, ..., vin , vq and so on
Simple Path: path in which all vertices, exceptpossibly the first and last, are distinct (0, 1, 3,2) in G1 and (0, 1, 3, 1) First simple
Simple directed is (0, 1, 2) in G3
Connected: Undirected graph, two vertices v0
and v1, are connected if there is a path in Gfrom v0 to v1
An undirected graph is connected if for everypair of distinct vertices vi, vj, there is a pathfrom vi to vj in G.
G1 and G2 connected.
1. Connected component (component)(undirected) is a maximal connected subgraph.
2. A tree is a graph that is connected and acyclic3. A directed graph is strongly connected if, for
every pair of vertices vi, vj in V(G), there is adirected path from vi to vj and also from vj to vi.
© 2015 JW Ryder
CSCI 203 Data Structures 7
Definitions continued
Degree of a vertex is the number of edges inc identto that vertex
Directed graphs have both in and out degrees General parlance: digraph = directed graph; graph
means undirected graph
© 2015 JW Ryder
CSCI 203 Data Structures 8
bfs (), dfs ()
• Ways to cover all connected vertices, a way to explore
• What does O(e) mean?• Is O(e) better than O(n)?
• 0 < O(1) < O(e) <= O(n2)• Algorithm is dependent on the
number of edges as far as its performance is concerned
© 2015 JW Ryder
CSCI 203 Data Structures 9
Connected Components
void connected (void) {
// determine connected components of graph
int i;
for (i = 0; i < n; i++)
if (!visited [i]) {
dfs (i);
cout << endl;
}// end then
} // end connected
© 2015 JW Ryder
CSCI 203 Data Structures 10
Spanning Trees• bfs () or dfs () implicitly
partitions edges of G into 2 sets– T - Tree edges– N - Non-Tree edges
• T is the set of edges traversed during execution and N are the rest
• Can determine (save) edge list by recording edges as algorithm arrives at each
• T then represents the head of the traversed edge list
© 2015 JW Ryder
CSCI 203 Data Structures 11
Spanning Tree Definition
• Any tree that consists solely of edges in G and that includes all the vertices in G
• May use dfs () or bfs () to create a spanning tree
• When dfs () used, spanning tree known as dfs spanning tree
• When bfs () used, what is it called?
© 2015 JW Ryder
CSCI 203 Data Structures 12
Adding N edges
• Suppose we add a non-tree edge (v, w) into any spanning tree T
• Result ==> cycle created consisting of edge (v,w) and all edges on the path from w to v in T
• Show example with one of the drawn spanning trees
© 2015 JW Ryder
CSCI 203 Data Structures 13
Minimum Cost Spanning Trees (MST)
• The cost of a spanning tree of a weighted undirected graph is the sum of the costs (weights) of the edges in the spanning tree.
• A minimum cost spanning tree is a spanning tree of least cost
© 2015 JW Ryder
CSCI 203 Data Structures 14
3 Common MST Algorithms
• Kruskal• Prim• Sollin• All three use a design method
called the Greedy Method
© 2015 JW Ryder
CSCI 203 Data Structures 15
Greedy Method
• Construct an optimal solution in stages
• At each stage we make a decision that is the best decision (using some criterion)
• Cannot change decision later so the decision must result in a feasible solution
• Can be applied to wide variety of problems
© 2015 JW Ryder
CSCI 203 Data Structures 16
Selection Criteria
• Selection of item at each stage is based on either least cost or highest profit criterion
• Feasible solution is one which works within the constraints specified by problem
© 2015 JW Ryder
CSCI 203 Data Structures 17
MST - Least Cost
• Solution must– use only edges within the graph G– use exactly n - 1 edges– may not use edges that would
produce a cycle
© 2015 JW Ryder
CSCI 203 Data Structures 18
Kruskal’s Algorithm
• Builds a MST by adding edges to T one at a time
• Selects edges in T in non-decreasing order of cost
• Edge added to T if it does not form a cycle with edges that are already in T
• G is connected, has n > 0 vertices, exactly n - 1 edges will be selected to include in T
© 2015 JW Ryder
CSCI 203 Data Structures 19
Kruskal Continued
• Draw and do example (only talking no algorithm)
• Initially, E is set of all edges in G
© 2015 JW Ryder
CSCI 203 Data Structures 20
Kruskal’s Algorithm
T = { };
while (T has < n-1 edges) && (E not Empty) {
choose a least cost edge (v,w) from E;
delete (v,w) from E;
if ((v,w) doesn’t create a cycle in T)
add (v,w) to T;
else discard (v,w);
} // end while
if (T contains < n-1 edges)
printf (“No Spanning Tree\n”);
© 2015 JW Ryder
CSCI 203 Data Structures 21
Kruskal Particulars• Must be able to determine edge with
minimum cost edge and delete it– Min-heap; determine and delete next
least cost edge in O(lg e); can sort in O(e lg e)
• To check that new edge (v,w) doesn’t create cycle in T and to add edge to T, use union-find operations. – View each connected component in T
as a set containing the vertices in that component
– Initially T is empty, each vertex of G is separate set
– Before adding edge (v,w), use find operation to determine if v and w are in same set• Yes? 2 vertices already connected• No? add edge
© 2015 JW Ryder
CSCI 203 Data Structures 22
Shortest Path
• Graph represents highway system
• Edges have weights• Length of a path is sum of
weights of edges along path• Starting vertex is source, ending
vertex is destination• One way streets are possible;
digraph• Weights are positive
© 2015 JW Ryder
CSCI 203 Data Structures 23
Single Source All Destinations
• Directed Graph, G = (V, E)• Weighting function w(e),
w(e) > 0• Source vertex is V0
© 2015 JW Ryder
CSCI 203 Data Structures 24
Graph in Figure 1• Shortest path from V0 to all
other vertices• V0 to V1 • Greedy algorithm to generate
shortest paths• S is set of vertices whose
shortest paths have been found• For w not is S, distance[w] is
length of shortest path starting from V0 going through vertices only in S and ending at w
• Generate paths in non-decreasing order of lengths
© 2015 JW Ryder
CSCI 203 Data Structures 25
Observation 1• If the next shortest path is to vertex u, then
the path from V0 to u goes through only those vertices that are in S– Show all intermediate vertices on the
shortest path from V0 to u are already in S
– Assume vertex w on path not in S– Path from V0 to u must go through w
and length from V0 to w must be less than length from V0 to u
– Since paths generated in non-decreasing order we must have generated path to w before
– Contradiction!– Ergo cannot be any intermediate vertex
that is not in S
© 2015 JW Ryder
CSCI 203 Data Structures 26
Observation 2
• Vertex u is chosen so that it has the minimum distance among all vertices not in S
• If several vertices with same distance the choice is not important
© 2015 JW Ryder
CSCI 203 Data Structures 27
Observation 3• Once u is selected and the shortest
path has been generated from V0 to u, u becomes a member of S
• Adding u to S can change the shortest paths starting at V0 , going through vertices only in S, and ending at vertex w that is not currently in S
• If distance changes, we have found a shorter path from V0 to w passing through u– Intermediate vertices are in S, subpath
from u to w can be chosen so that there are no intermediate vertices
– The length of the shorter path is distance[u] + length(<u, w>)
© 2015 JW Ryder
CSCI 203 Data Structures 28
Who made these Observations?
• Observations and Algorithm credited to Edsger Dijkstra
© 2015 JW Ryder
CSCI 203 Data Structures 29
Mechanics• n vertices numbered from 0 to n-1• Set S maintained as an array with
name found[ ]– found[i] = TRUE (1) if vertex i in S
else FALSE (0)
• Graph represented by its cost adjacency matrix– cost [i][j] = weight of edge <i, j>– If edge <i, j> not in G set cost [i][j] to
some large number• Choice of number is arbitrary but can’t
overflow sign• Must be > any other number in matrix• distance [u] + cost [u][w] can’t produce
overflow
© 2015 JW Ryder
CSCI 203 Data Structures 30
Shortest Path Parameters• Draw distance [ ], found [ ],
cost [ ] [ ], n, v for Figure 1 • Draw figures on board, all based
on n
V0V1 V4
V5V3V2
45
50 10
20 10 15 20 35 30
15 3
Figure 1
© 2015 JW Ryder
CSCI 203 Data Structures 31
Basic Idea with WordsInitialize set S to Empty
Initialize distance[ ] to the distances for the source vertex from cost matrix
Put v into S; Make sure distance to self is 0
For i = 0 to Number of Vertices in G - 2 do
Choose next vertex u to add to S. Will be vertex from the set
E-S with the shortest distance
Add u to S
For w = each vertex in G // 0 .. n-1
if w is not already in S
if distance from v to u then to w < current shortest to w
Make shortest distance from v through u to w for
distance [w]
endif
endif
endfor
endfor
© 2015 JW Ryder
CSCI 203 Data Structures 32
choose ( )int choose (int distance [ ], int n, short int found [ ] )
{ // Find smallest distance not yet checked
int i, min, minpos;
min = INT_MAX;
minpos = -1;
for (i = 0; i < n; i++)
if (distance [i] < min && !found [i]) {
min = distance [i];
minpos = i;
}
return minpos;
} // end choose ( )
© 2015 JW Ryder
CSCI 203 Data Structures 33
shortestpath ( )void shortestpath (int v, int cost [ ][MAX_VERTICES], int distance [ ], int n, short int found [ ])
{ // distance [i] is shortest path from vertex v to i, found [i] holds a 0 if shortest path for vertex i has not been found else 1, cost is the cost adjacency matrix
int i, u, w;
for (i = 0; i < n; i++) { // Initialize part
found [i] = FALSE;
distance [i] = cost [v][i];
} // end for
found [v] = TRUE;
distance [v] = 0;
for (i = 0; i < n-2; i++) {
u = choose (distance, n, found); // Pick next vertex to add to S
found [u] = TRUE; // Add it to S
for (w = 0; w < n; w++) { // Check all vertices adjacent to
if (!found [w]) // u but not yet in S
if (distance [u] + cost [u][w] < distance [w])
distance [w] = distance [u] + cost [u][w];
} // end for
} // end for
© 2015 JW Ryder
CSCI 203 Data Structures 34
Shortest Path Analysis
• O(n2) n vertices, adjacency matrix• First for loop takes O(n)• Second for loop executed n-2 times -
O(n)– This loop needs O(n) - each execution
• choose () - O(n)• Update distance [ ] - O(n)
– Total time needed is O(n2)
• Each edge in G examined at least once O(e)– Minimum possible with cost adjacency
O(n2)