Professional Documents
Culture Documents
Chapter 8, Part II: Graph Algorithms
Chapter 8, Part II: Graph Algorithms
Chapter 8, Part II: Graph Algorithms
Graph Algorithms
On each pass, we will choose the smallest of the edges from nodes already in the MST to nodes not in the MST (the fringe)
Dijkstra-Prim Example
Dijkstra-Prim Example
Dijkstra-Prim Example
Dijkstra-Prim Example
end while
10
Kruskal Example
11
Kruskal Example
12
Kruskal Example
13
Kruskal Example
14
and the end node of this edge are in partitions with different roots. Adding this edge will not create a cycle. We merge these two partitions to form a single partition.
15
Shortest-Path Algorithm
The shortest-path algorithm finds the series of edges between two nodes that has the smallest total weight
16
17
Dijkstras Method
This is similar to the Dijkstra-Prim MST algorithm, but
instead of just looking at the single shortest edge from a node to the fringe, we look at the shortest path from the start node to a fringe node
18
Dijkstras Algorithm
Select a starting node build the initial fringe from nodes connected to the starting node while we are not at the destination node do choose the fringe node with the shortest path to the starting node add that node and its edge to the tree update the fringe by: adding nodes that are connected to the new node to the fringe for each node in the fringe do update its edge to the one connected to the tree on the shortest path to the starting node end for end while
19
destination
20
21
22
23
Biconnected Components
A biconnected component of a graph is a set of nodes for which there is more than one path between each of the nodes A biconnected component can also be just two nodes as well
24
25
Articulation Points
An articulation point in a graph is a node that if removed would cause the graph to no longer be connected
Articulation points are the points where the graph can be broken down into its biconnected components
26
27
A Better Method
If we do a depth-first traversal, when we reach a dead end, the adjacent visited nodes show us how far back in the graph we could go This identifies the portion of the graph that has two different paths and is biconnected
28
A Better Method
Keep a count of how many nodes have been visited and use this to assign an order number to each of the nodes When we get to a dead end, we look at all of the order numbers for the adjacent nodes and pick the smallest one as the back index We return the smallest back index to the previous node in the traversal
29
A Better Method
When we get the back index values from each of the adjacent nodes we visited from the current node, we look at the smallest of these If it is the same as or larger than the current nodes order number, we are at an articulation point and the nodes just visited are a biconnected component
30
Partitioning a Set
We may need to have a set of numbers partitioned so that each number appears in exactly one subset Programming language set data structures will not work because they cannot guarantee that each element will be in only one set This partitioning ability is necessary for algorithms like Kruskals MST algorithm
31
Partitioning Method
We have an array with one element for each of the set values All of the array values are initialized with -1 to show that they are the root of a partition with just one element
As we combine partitions, we will replace these values with an ancestor in the combined partition and the root will have the total number of elements in the partition
32
3 nodes 7 nodes
2 nodes
33
Union:
// i and j are the partitions to join together
totalElements = Parent[i] + Parent[j] // negative if (Parent[i] Parent[j]) then // P_i has fewer nodes Parent[i] = j // j becomes the parent of i
// j is the root of the combined partition
Parent[j] = totalElements else // P_j has fewer nodes Parent[j] = i Parent[i] = totalElements end if
34
// If s is not a root, we update result to be the parent of s and // check if the new result is the root.
// We continue to work up this partition until the root is reached.
35