+ All Categories
Home > Documents > chapter_08_part2.ppt

chapter_08_part2.ppt

Date post: 14-Apr-2018
Category:
Upload: anonymous-boresf
View: 218 times
Download: 0 times
Share this document with a friend
35
Chapter 8, Part II Graph Algorithms
Transcript
Page 1: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 1/35

Chapter 8, Part II

Graph Algorithms

Page 2: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 2/35

2

Minimum Spanning Tree

• The minimum spanning tree (MST) of aweighted connected graph is a subgraph thatcontains all of the nodes of the original graphand a subset of the edges so that:

 – The subgraph is connected

 – The total weights of the subgraph edges is thesmallest possible

Page 3: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 3/35

3

Brute Force Algorithm

• We could look at each subset of the edge setand first see if it is connected and then seeif its total weight is smaller than the bestanswer so far 

• If there are N edges in the graph, this processwill require that we look at 2N  differentsubsets

• This is a lot more work than is needed

Page 4: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 4/35

4

The Dijkstra-Prim Method

• This is a greedy algorithm that solves the larger problem by looking at a subset and making the bestchoice 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 theedges from nodes already in the MST to nodes notin the MST (the fringe)

Page 5: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 5/35

5

Dijkstra-Prim Example

Page 6: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 6/35

6

Dijkstra-Prim Example

Page 7: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 7/35

7

Dijkstra-Prim Example

Page 8: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 8/35

8

Dijkstra-Prim Example

Page 9: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 9/35

9

The Dijkstra-Prim Algorithm

Select a starting node build the initial fringe from nodes

connected to the starting node

 while there are nodes left dochoose the edge to the fringe of the smallest weight

add the associated node to the treeupdate 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

Page 10: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 10/35

10

The Kruskal Method

• Kruskal’s algorithm concentrates on the edgesinstead of the nodes

• First the edges are sorted in order of nondecreasingweight

• Edges are added to the MST as long as they do notform 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

Page 11: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 11/35

11

Kruskal Example

Page 12: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 12/35

12

Kruskal Example

Page 13: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 13/35

13

Kruskal Example

Page 14: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 14/35

14

Kruskal Example

Page 15: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 15/35

15

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) thenadd edge[e] to spanning treeincludedCount = includedCount + 1

Union( parent1, parent2 )end if

e = e + 1 end while

If parent1 is not the same as parent2, we know that the start node

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.

Page 16: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 16/35

16

Shortest-Path Algorithm

• The shortest-path algorithm finds the

series of edges between two nodes that

has the smallest total weight

Page 17: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 17/35

17

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

Page 18: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 18/35

18

Dijkstra’s Method 

• This is similar to the Dijkstra-Prim MSTalgorithm, but

instead of  just looking at the single shortestedge from a node to the fringe,

we look at the shortest path from the startnode to a fringe node

Page 19: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 19/35

19

Dijkstra’s 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 pathto 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 doupdate its edge to the one connected to

the tree on the shortest path to the

starting node

end for

end while

Page 20: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 20/35

20

Dijkstra’s Shortest-Path Example

source

destination

Page 21: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 21/35

21

Dijkstra’s Shortest-Path Example

Page 22: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 22/35

22

Dijkstra’s Shortest-Path Example

Page 23: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 23/35

23

Dijkstra’s Shortest-Path Example

Page 24: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 24/35

24

Biconnected Components

•  A biconnected component of a graph is

a set of nodes for which there is more

than one path between each of thenodes

•  A biconnected component can also be

 just two nodes as well

Page 25: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 25/35

25

Biconnected Components Example

• The following graph

has three biconnected components:

Page 26: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 26/35

26

 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

Page 27: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 27/35

27

Brute Force Method

• Remove one node at a time and use a traversalmethod 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

Page 28: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 28/35

28

 A Better Method

• If we do a depth-first traversal, when we reach adead end, the adjacent visited nodes show us

how far back in the graph we could go

• This identifies the portion of the graph that hastwo different paths and is biconnected

Page 29: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 29/35

29

 A Better Method

• Keep a count of how many nodes have been visited anduse 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 oneas the back index

• We return the smallest back index to the previous node inthe traversal

Page 30: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 30/35

30

 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 node’s

order number, we are at an articulation point and thenodes just visited are a biconnected component

Page 31: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 31/35

31

Partitioning a Set

• We may need to have a set of numbers partitioned sothat each number appears in exactly one subset

• Programming language set data structures will not workbecause they cannot guarantee that each element willbe in only one set

• This partitioning ability is necessary for algorithms likeKruskal’s MST algorithm 

Page 32: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 32/35

32

Partitioning Method

• We have an array with one element for each of the setvalues

•  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 thepartition

Page 33: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 33/35

33

Partitioning Method Example

3 nodes

7 nodes 2 nodes

Node 5 is the

immediate parent

of node 1

Page 34: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 34/35

34

Partitioning Sets Algorithms

• Initialization:

for i = 1 to N doParent[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 nodesParent[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] = iParent[i] = totalElements

end if

Page 35: chapter_08_part2.ppt

7/27/2019 chapter_08_part2.ppt

http://slidepdf.com/reader/full/chapter08part2ppt 35/35

35

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.


Recommended