Docsity

CS 357: Data Structures - Transitive Closure, Floyd-Warshall, DAGs, Topological Ordering, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS 357: Data Structures
Class 33
April 29, 2002
Spring 2002 CS 357 Class Notes Page 2
Announcements
• Next time, finish chapter 12.
• Friday is the review. Come with questions.
• Project 6 is due Saturday.
• Project 3 scores will be posted tomorrow.
• Project 4 will be handed back in class on
Wednesday.
• Finals are scheduled for EE 119:
– May 9 from 2-4:30p for Section 1
– May 7 from 2-4:30p for Section 2
Spring 2002 CS 357 Class Notes Page 3
Transitive Closure
• The transitive closure of a digraph G, denoted
G*, is a digraph with the same vertices and
paths that adds all edges (u,v) such that v is
reachable from u in G.
CS 114
CS 124
ECE 380CS 325
ECE 383
CS 114
CS 124
CS 325
ECE 380
ECE 383
G*G*G
Spring 2002 CS 357 Class Notes Page 4
Sample Graph
Spring 2002 CS 357 Class Notes Page 5
Sample Transitive Closure
Spring 2002 CS 357 Class Notes Page 6
Floyd-Warshall Algorithm
• Computes the transitive closure of a graph.
• Begin with the original graph.
• Create n subsequent graphs where:
– For each, copy the previous graph.
– Then, add new edges where the previous graph
contained a path of length 2.
G
x
G
x+1
pf3
pf4

Partial preview of the text

Download CS 357: Data Structures - Transitive Closure, Floyd-Warshall, DAGs, Topological Ordering and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 357: Data Structures

Class 33

April 29, 2002

Spring 2002 CS 357 Class Notes (^) Page 2

Announcements

• Next time, finish chapter 12.

• Friday is the review. Come with questions.

• Project 6 is due Saturday.

• Project 3 scores will be posted tomorrow.

• Project 4 will be handed back in class on

Wednesday.

• Finals are scheduled for EE 119:

  • May 9 from 2-4:30p for Section 1
  • May 7 from 2-4:30p for Section 2

Spring 2002 CS 357 Class Notes (^) Page 3

Transitive Closure

• The transitive closure of a digraph G, denoted

G*, is a digraph with the same vertices and

paths that adds all edges (u,v) such that v is

reachable from u in G.

CS 114

CS 124

CS 325 ECE 380

ECE 383

CS 114

CS 124

CS 325

ECE 380

ECE 383

G GG

Spring 2002 CS 357 Class Notes (^) Page 4

Sample Graph

Spring 2002 CS 357 Class Notes Page 5

Sample Transitive Closure

Spring 2002 CS 357 Class Notes Page 6

Floyd-Warshall Algorithm

• Computes the transitive closure of a graph.

• Begin with the original graph.

• Create n subsequent graphs where:

  • For each, copy the previous graph.
  • Then, add new edges where the previous graph

contained a path of length 2.

G (^) x G (^) x+

Spring 2002 CS 357 Class Notes (^) Page 7

Floyd-Warshall Algorithm

Algorithm FloydWarshall(G): let v 1 , v 2 , … v (^) n be an arbitrary numbering of the vertices in G

G 0  G for k  1 to n do G (^) k  G (^) k- for each i and j in {1…n} with i≠j and i&j≠k do if both edges (v (^) i,v (^) k) and (v (^) k,v (^) j ) are in G (^) k-1 then add edge (v (^) i,v (^) j) to Gk if it does not exist return G (^) n

vi

vk vj vi

vk vj

G (^) x G (^) x+

Spring 2002 CS 357 Class Notes (^) Page 8

Quick Quiz

• For the following digraph (G 0 ), list the edges

for G 1 and G 2 using Floyd-Warshall.

v (^6)

v (^3)

v (^4)

v (^1)

v 2 v (^5)

v (^7)

Spring 2002 CS 357 Class Notes (^) Page 9

Directed Acyclic Graphs (DAGs)

• Digraphs without directed cycles.

• Useful for representing:

  • Inheritance
  • Prerequisites
  • Scheduling constraints

Spring 2002 CS 357 Class Notes (^) Page 10

Topological Ordering

  • A useful means of identifying relative priority or order

among vertices in a DAG.

  • Order vertices in a DAG such that each has a value

smaller than that of its immediate descendents.

  • In other words, for every edge (u,v), order(u) <

order(v).

CS 114

CS 124

CS 325 ECE 380

ECE 383

Spring 2002 CS 357 Class Notes Page 11

Topopseudo

Algorithm Topological Sort(G): Let S be an initially empty stack. for each vertex u in G do Let incounter(u) be the in-degree of u. if incounter(u) = 0 then S.push(u)

i  1 while S is not empty do u  S.pop() Let u be vertex number i in the ordering i  i + 1 for each outgoing edge (u,w) of u do incounter(w)  incounter(w) – if incounter(w) = 0 then S.push(w) Spring 2002 CS 357 Class Notes Page 12

Quick Quiz

  • Given the digraph to

the right, fill in the

question marks with

the node’s

topological order as

calculated by the

algorithm given.

  • Note, the numbers

are the incounter

values as assigned

after the first for loop.

Spring 2002 CS 357 Class Notes (^) Page 19

Quick Quiz

  • Find the shortest path from ORD to all other airports

using Dijkstra’s Algorithm. At each step, show the

cloud, the priority queue, and the distance vector.

Spring 2002 CS 357 Class Notes (^) Page 20

Quick Quiz

  • Change all edge weights to 1 and repeat.
    • Find the shortest path from ORD to all other airports using Dijkstra’s Algorithm. At each step, show the cloud, the priority queue, and the distance vector.

Spring 2002 CS 357 Class Notes (^) Page 21

Minimum Spanning Trees

  • Perhaps you want to find a free tree that contains all

vertices and has the minimum total weight possible

over all such trees.

  • Application? Maybe you want to connect all

computers in an office complex with the least amount

of cable possible.

  • We will see two greedy solutions:
    • Kruskal’s Algorithm
    • Prim-Jarník Algorithm
  • For demonstration, will simplify as before using

undirected, simple graphs.