1 graphs and graph algorithms. 2 graph traversals zdepth first traversal. ystarting from a source...
Post on 22-Dec-2015
218 views
TRANSCRIPT
1
Graphs and Graph Algorithms
2
Graph Traversals
Depth First Traversal. Starting from a source node, mark the
node as visited Choose an ordering of the edges from
this node. Do a Depth First Traversal of the vertex
the first edge is connected to, provided the vertex has not yet been visited.
Repeat the previous step for all edges.
3
Depth First Traversal From Vertex A
Assume ordering is alphabetical.From A, go to B.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
4
Depth First Traversal From Vertex A
From B, go to D.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
5
Depth First Traversal From Vertex A
From D, go to C.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
6
Depth First Traversal From Vertex A
From C, go to E.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
7
Depth First Traversal From Vertex A
From E, go to F.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
8
Depth First Traversal From Vertex A
Done.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
9
Breadth First Traversal
Mark the source vertex as visitedVisit all the children of the source
(provided they have not already been visited)
Perform a Breadth First Traversal of all of the children.
10
Breadth First Traversal From Vertex A
Assume ordering is alphabetical.From A, go to B.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
11
Breadth First Traversal From Vertex A
From A, go to C.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
12
Breadth First Traversal From Vertex A
From B, go to D.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
13
Breadth First Traversal From Vertex A
From B, go to E.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
14
Breadth First Traversal From Vertex A
From B, go to F.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
15
Breadth First Traversal From Vertex A
Done.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
16
Definitions of Connectivity
An undirected graph is connected if there is a path from v to w for all vertices v and w in the vertex set.
A directed graph is strongly connected if there is a path from v to w for all vertices v and w in the vertex set.
A directed graph is weakly connected if its underlying undirected graph is connected.
17
Underlying Undirected Graphs
Every directed graph has an underlying undirected graph.
This graph is found as follows: It has the same vertices as the directed graph If the directed graph has an edge (v,w), then
the underlying undirected graph has edges (v,w) and (w,v).
Simply put, to find the underlying undirected graph, remove the arrows.
18
Testing for Connectivity
For undirected graphs: Apply a DFT or a BFT from any vertex. If all
vertices are visited, the graph is connected.For weakly connected directed graphs:
Create the underlying undirected graph and test it for connectivity as above.
For strongly connected directed graphs: Apply a DFT or a BFT from every vertex. If all
vertices are reachable every time, the graph is strongly connected.
19
Dijkstra’s Algorithm
Start with the Finished Set of Nodes containing only the source.
Find all nodes not in the Finished Set of Nodes, but connected to one of these nodes. Call this set Next Nodes.
Find the node in the set of Next Nodes which has minimum cost to link to the Finished Set of Nodes.
Add this node to the Finished Set of Nodes
20
Dijkstra’s Algorithm II
Repeat the previous 3 steps until all nodes are added to the Finished Set of Nodes.
Note: To determine the minimum cost to link a node: Let PrevNode be the one currently in the
Finished set of Nodes; Let ThisNextNode be the one it is linked to. The cost of ThisNextNode = cost of PrevNode
+ cost of the link between the two.
21
Dijkstra’s Algorithm Example
Suppose A wishes to send to F. What is the cheapest path?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
22
Dijkstra’s Algorithm Example
Tan nodes are in the Finished Set.Blue nodes are in the Next Set.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
23
Dijkstra’s Algorithm Example
What happens next?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
24
Dijkstra’s Algorithm Example
C is least cost among Next Node set, so it is added to Finished set.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
How do I update the Next Node Set?
25
Dijkstra’s Algorithm Example
D, E, F added to Next Node Set. What are their costs?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
4
7
8
What is next for Finished Set of nodes?
26
Dijkstra’s Algorithm Example
B is added to Finished set of nodes.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
4
7
8
How do I update the Next Set of Nodes?
27
Dijkstra’s Algorithm Example
E’s minimum changes to 6.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
4
6
8
Next for Finished Set?
28
Dijkstra’s Algorithm Example
Next, update Next Set costs:
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
4
6
8
D has minimum cost.
29
Dijkstra’s Algorithm Example
Next for Finished Set?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
4
6
6
30
Dijkstra’s Algorithm Example
No cost updates for Next Nodes here; finish it up:
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
4
6
6
Either E or F; I chose E.
31
Dijkstra’s Algorithm Example
All nodes in finished set...done
A
B
C
D
E
F
2
1
3
74
3 7
6
2
60
2
1
4
6
6
32
Dijkstra Discussion
This example is centralized; can we make it distributed? Each node would need the entire graph
before starting. Each node would need to send its
connection costs to everyone else.Can we make it adaptive?
When costs change, they would need to be sent to everyone and the algorithm rerun.
33
Bellman-Ford Algorithm
Basic idea: Keep track of current minimum cost to
all other nodes . When the minimum cost to another
node from one of your neighbors changes, maybe you can change your cost to that node too.
Initially, you only know the cost to your neighbors.
34
Bellman-Ford Explanation
Thus, the minimum cost from A to F is the minimum of: The cost from A to B plus the cost from B to F; The cost from A to C plus the cost from C to F.
Thus, each source node can fill in its destination minimums by looking at its neighbor’s minimums.
This process iterates until no changes are made to the list of minimums.
35
Bellman-Ford Example I
This is the same graph as for Dijkstra’s Algorithm and the initial table.
A
B
C
D
E
F
2
1
37
4
3 7
6
2
6
A B C D E FA NA B,2 C,1 ? ? ?B A,2 NA ? D,3 E,4 F,7C A,1 ? NA D,3 E,6 F,7D ? B,3 C,3 NA ? F,2E ? B,4 C,6 ? NA F,6F ? B,7 C,7 D,2 E,6 NA
36
Bellman-Ford Example II
A B C D E FA NA B,2 C,1 ? ? ?B A,2 NA ? D,3 E,4 F,7C A,1 ? NA D,3 E,6 F,7D ? B,3 C,3 NA ? F,2E ? B,4 C,6 ? NA F,6F ? B,7 C,7 D,2 E,6 NA
A B C D E FA NA B,2 C,1 C,4 B,6 C,8B A,2 NA A,3 D,3 E,4 D,5C A,1 A,3 NA D,3 E,6 D,5D C,4 B,3 C,3 NA B,7 F,2E B,6 B,4 C,6 B,7 NA F,6F C,8 D,5 D,5 D,2 E,6 NA
First Iteration (Repeated):
Second Iteration:
37
A B C D E FA NA B,2 C,1 C,4 B,6 C,8B A,2 NA A,3 D,3 E,4 D,5C A,1 A,3 NA D,3 E,6 D,5D C,4 B,3 C,3 NA B,7 F,2E B,6 B,4 C,6 B,7 NA F,6F C,8 D,5 D,5 D,2 E,6 NA
Bellman-Ford Example III
Second Iteration (Repeated):
Third Iteration:A B C D E F
A NA B,2 C,1 C,4 B,6 C,6B A,2 NA A,3 D,3 E,4 D,5C A,1 A,3 NA D,3 E,6 D,5D C,4 B,3 C,3 NA B,7 F,2E B,6 B,4 C,6 B,7 NA F,6F D,6 D,5 D,5 D,2 E,6 NA
38
Bellman-Ford Discussion
This example is centralized; can we make it distributed? Each node needs to send its costs to its
neighbors only. In our example, A only needs the rows
for B and C to decide on its cost to all destinations.
39
Bellman-Ford Discussion II
Can we make it adaptive? Each node keeps the costs from its
neighbors; when these costs change, it only needs to pick the minimum among the old costs and the new one.
NOTE: when a cost changes, a node must notify its neighbors.
40
Minimum Cost Spanning Trees
Prim’s Algorithm May be viewed as a “vertex-centric”
approach. Basically the same as Dijkstra’s except that
the cost of a node is its minimum cost connection to the finished set, rather than minimum connection to source vertex.
Proceed with adding the minimum vertex to the finished set until all vertices are added.
41
Prim’s Algorithm Example
Same graph as for Dijkstra’s Algorithm, but let’s start at D.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
42
Prim’s Algorithm Example
Next for Finished Set?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
3
3
0
2
43
Prim’s Algorithm Example
Insert F.Next for Finished Set?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
3
3
0
2
6
44
Prim’s Algorithm Example
Insert B. Note change of E’s weight.Next for Finished Set?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
3
3
0
2
4
2
45
Prim’s Algorithm Example
Insert A. Note change of C’s weight.Next for Finished Set?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
3
1
0
2
4
2
46
Prim’s Algorithm Example
Insert C. Next for Finished Set?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
3
1
0
2
4
2
47
Prim’s Algorithm Example
Insert E. Done.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
3
1
0
2
4
2
48
Prim’s Discussion
This is not the same minimum spanning tree as produced by Dijkstra’s Algorithm.
Starting from node A, we added (C,D) rather than (B,D).
Both edges have the same weight, so the overall cost is the same.
This means that there may be more than one minimum spanning tree!
49
Kruskal’s Algorithm
More of an “edge-centric” approach.Idea: sort the edges; add edges from
least weight to maximum weight with the restriction that the newly added edge does not create a cycle.
50
Kruskal’s Algorithm Example
Which edge has minimum weight?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
51
Kruskal’s Algorithm Example
Edge (A,C) is minimum. What’s next?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
52
Kruskal’s Algorithm Example
Edge (A,B) is minimum. What’s next?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
53
Kruskal’s Algorithm Example
Edge (D,F) is minimum. What’s next?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
54
Kruskal’s Algorithm Example
Edge (B,D) is minimum. What’s next?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
55
Kruskal’s Algorithm Example
Edge (C,D) is minimum, but creates a cycle. Here, we choose (B,E). What’s next?
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
56
Kruskal’s Algorithm Example
All vertices are now in the tree, so we are done.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
57
Kruskal’s Algorithm -- Detecting Cycles
Neat algorithm, but how are cycles detected?
Answer: Start by considering each vertex in its
own distinct set. Whenever an edge (v,w) is considered, v
and w must be in different vertex sets. When adding edge (v,w), merge the sets
containing v and w into one set.
58
Kruskal’s Algorithm Example with edge sets
Vertex Sets: ({A},{B},{C},{D},{E},{F}}
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
59
Kruskal’s Algorithm Example
Vertex Sets: ({A,C},{B},{D},{E},{F}}
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
60
Kruskal’s Algorithm Example
Vertex Sets: ({A,B,C},{D},{E},{F}}
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
61
Kruskal’s Algorithm Example
Vertex Sets: ({A,B,C},{D,F},{E}}
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
62
Kruskal’s Algorithm Example
Vertex Sets: ({A,B,C,D,F},{E}}
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
63
Kruskal’s Algorithm Example
Note that (C,D) is minimum, but C and D are in the same vertex set.
Vertex Sets: ({A,B,C,D,F},{E}}
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
64
Kruskal’s Algorithm Example
Vertex Sets: ({A,B,C,D,E,F}}All vertices are now in one set, so
we are done.
A
B
C
D
E
F
2
1
3
74
3 7
6
2
6
65
The End Slide