Chapter 8, Part II: Graph Algorithms

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

Chapter 8, Part II

Graph Algorithms

Minimum Spanning Tree


The minimum spanning tree (MST) of a weighted connected graph is a subgraph that contains all of the nodes of the original graph and a subset of the edges so that:
The subgraph is connected The total weights of the subgraph edges is the smallest possible

Brute Force Algorithm


We could look at each subset of the edge set and first see if it is connected and then see if its total weight is smaller than the best answer so far If there are N edges in the graph, this process will require that we look at 2N different subsets This is a lot more work than is needed

The Dijkstra-Prim Method


This is a greedy algorithm that solves the larger problem by looking at a subset and making the best choice for it In this case, we will look at all of the edges from the starting node and choose the smallest

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

The Dijkstra-Prim Algorithm


Select a starting node build the initial fringe from nodes connected to the starting node while there are nodes left do
choose the edge to the fringe of the smallest weight add the associated node to the tree update the fringe by: adding nodes to the fringe connected to the new node updating the edges to the fringe so that they are the smallest

end while

The Kruskal Method


Kruskals algorithm concentrates on the edges instead of the nodes First the edges are sorted in order of nondecreasing weight Edges are added to the MST as long as they do not form a cycle with edges added in previous passes When all of the nodes are connected and there are N 1 edges in the MST, we are done

10

Kruskal Example

11

Kruskal Example

12

Kruskal Example

13

Kruskal Example

14

The Kruskal Algorithm


sort the edges in nondecreasing order by weight initialize partition structure e = 1 includedCount = 0 // # of edges currently included in MST while (e E and includedCount N-1) do parent1 = FindRoot(edge[e].start ) parent2 = FindRoot(edge[e].end ) if (parent1 parent2) then add edge[e] to spanning tree includedCount = includedCount + 1 Union( parent1, parent2 ) end if e = e + 1 If parent1 is not the same as parent2, we know that the start node end while

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

MST vs. Shortest Path


The MST algorithm can skip an edge of larger weight but include many edges of smaller weight that results in a longer path than the single edge

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

Dijkstras Shortest-Path Example


source

destination

20

Dijkstras Shortest-Path Example

21

Dijkstras Shortest-Path Example

22

Dijkstras Shortest-Path Example

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

Biconnected Components Example


The following graph

has three biconnected components:

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

Brute Force Method


Remove one node at a time and use a traversal method to see if the graph is still connected If the graph is still connected, the removed node is not an articulation point If the graph is no longer connected, the removed node is an articulation point

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

Partitioning Method Example

Node 5 is the immediate parent of node 1

3 nodes 7 nodes

2 nodes

33

Partitioning Sets Algorithms


Initialization:
for i = 1 to N do Parent[i] = -1 end do

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

Partitioning Sets Algorithms


Find Root (Find the root of a node s in a partition):
result = s while (Parent[result] > 0) do result = Parent[result] end while return result // If Parent[s] is negative, s is the root. The result of s is returned.

// 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

You might also like