wednesday 10/13

13
Wednesday 10/13 Announcements Jordan Taff – a moment of silence Homework 5 due today Program 3 (Graphs and BFS) due Friday, 10/15 Midterm Wednesday, 10/20 Next time Read Section 23.2: Kruskal and Prim algorithms for minimum spanning tree Today: Topological Sort (DAG – directed acyclic graph) Strongly Connected Components (digraph – directed graph) Review 9-GraphSCCPractice(20 minutes) Disjoint Sets

Upload: others

Post on 24-Dec-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Wednesday 10/13

Wednesday 10/13Announcements

Jordan Taff – a moment of silence

Homework 5 due today

Program 3 (Graphs and BFS) due Friday, 10/15

Midterm Wednesday, 10/20

Next time

Read Section 23.2: Kruskal and Prim algorithms for minimum spanning tree

Today:

Topological Sort (DAG – directed acyclic graph)

Strongly Connected Components (digraph – directed graph)

Review 9-GraphSCCPractice(20 minutes)

Disjoint Sets

Page 2: Wednesday 10/13

Topological Sort Topological Sort: Given a DAG list the nodes so that node ni comes before nj if

there is an edge (ni, nj)

Algorithm 1: Given DAG, G=(V,E) Step 1: Perform DFS Sweep to compute finish times. As each vertex is

finished, insert it at the front of a list. Step 2: Return the list

Algorithm 2: Given DAG, G=(V,E) While V is not empty

• Find a node, v, with in-degree zero and add v to the end of a list.• Remove v from V and all edges (v, w) from E

What is the running time of the two algorithms?

a b

e f

c d

g h

Page 3: Wednesday 10/13

SCC Algorithm

a) Run DFS and compute finish times

b) Find GT and run DFS on GT

starting with largest finish time

c) Resulting DAG of SCC

Page 4: Wednesday 10/13

Find SCC G=(V,E) below or 9-InClass Exercise

Trace the SCC Algorithm and process nodes in alphabetical order (q, r, s, t, …) for step 1

1. Perform dfs on G2. Let GT be the transpose of G (edges reversed)3. Step 3: Perform dfs on GT processing nodes with

largest finish times from step 1 first4. Step 4: The vertices of each tree in the dfs forest

found in Step 3 is a strongly connected component

Page 5: Wednesday 10/13

Union Find ADT

We have a universal set, U, and a collection of disjoint subsets S1, S2, …, Sk and Ui=1..k Si = U.

Each subset Si has a representative that is used to identify the subset.

Three basic operations

• makeSet(x) create a set with one member x

• find(x) return the set representative for x

• union(x, y) union the disjoint sets of x and y

Assume U = {1, 2, …, n}

Need to initialize with n makeSet( ) operations

At most n-1 union(x, y) operations

Assume there are m find(x) operations

• This is not how the book defines m

Page 6: Wednesday 10/13

Union Find Data Structure If we had a Union Find data structure, we could use it to find

the connected components of an undirected graph.

Find connected components of G(V, E) an undirected graph

1. For each v in V

makeSet(v)

2. For each (u, v) in E

if (find(u) != find(v))

union(u, v)

The resulting collection of disjoint sets correspond to the connected components of G(V, E)

Page 7: Wednesday 10/13

Simple Array Implementation

What is the running time for

n makeSet()

n-1 union(x, y)

m find(x)

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

1 2 3 1 5 3 1 8 9 8 8 9

Assume U={1, 2, …, n}

What will the n makeSet() operations do

How do you implement find(x)

How do you implement union(x, y)

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12][1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

1 2 3 4 5 6 7 8 9 10 11 12

union (1, 4) union (1, 7) union(8, 10) union (10, 11)

Page 8: Wednesday 10/13

Union Find Data Structures

Simple Array Implementation

Linked List Implementation (textbook)

Forest Implementation (textbook)

Page 9: Wednesday 10/13

Linked List Implementation Assume U={1, 2, …, n} and we have a “reference array”

What will the n makeSet() operations do – running time

How do you implement find(x) – running time for m find(x)

How do you implement union(x, y) – running time for n-1 union()

Use the weighted union heuristic – where do we store the size

Page 10: Wednesday 10/13

Forest Implementation

Assume U={1, 2, …, n} and we have a “reference array”

What will the n makeSet() operations do – running time

How do you implement find(x) – running time for m find(x)

How do you implement union(x, y) – running time for n-1 union()

Use the union by rank and path compression heuristics

Page 11: Wednesday 10/13

Forest Implementation with Rank

makeSet(x)

x.p = x

x.rank = 0

union(x, y)

link(find(x), find(y))

link(x, y)

if (x.rank > y.rank)

y.p = x

else x.p = y

if (x.rank==y.rank)

y.rank = y.rank+1

//trace this code include rank with

// each node

for x=1 to 6

makeSet(x)

union(2, 5)

union(4, 1)

union(6, 3)

union(5, 4)

union(5, 6)

Page 12: Wednesday 10/13

Forest Implementationwith Path Compression

find(x)

if (x != x.p)

x.p = find(x.p)

return x.p

Page 13: Wednesday 10/13

Union Find ImplementationsAssume we perform: n makeSet() - assume this is done firstm find() and n-1 union()

Our in-class array implementationΘ(m + n2)

Linked list Implementation using union() by sizeΘ(m + n log(n))

Disjoint-set forest implementation using union() by rank and path compression find()Θ( (n + m) α(n)) Where α(n) is the inverse of Ackermann’s function α(1080)=4

• 1080 ≅ the number of atoms in the observable universe