41
More Graph Algorithms Data Structures and Algorithms

More Graph Algorithms Data Structures and Algorithms

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: More Graph Algorithms Data Structures and Algorithms

More Graph Algorithms Data Structures and

Algorithms

Page 2: More Graph Algorithms Data Structures and Algorithms

Announcements

Page 3: More Graph Algorithms Data Structures and Algorithms

Problem 1: Ordering Dependencies

Today’s next problem: Given a bunch of courses with prerequisites, find an order to take the courses in.

Math 126

CSE 142

CSE 143

CSE 311

CSE 331

CSE 332

Page 4: More Graph Algorithms Data Structures and Algorithms

Problem 1: Ordering Dependencies

Given a directed graph G, where we have an edge from u to v if u must happen before v.

Can we find an order that respects dependencies?

Given: a directed graph G

Find: an ordering of the vertices so all edges go from left to right.

Topological Sort (aka Topological Ordering)

Uses:

Compiling multiple files

Graduating.

Page 5: More Graph Algorithms Data Structures and Algorithms

Topological Ordering

A course prerequisite chart and a possible topological ordering.

Math 126

CSE 142

CSE 143

CSE 311

CSE 331

CSE 332

Math 126 CSE 142 CSE 143 CSE 311 CSE 331 CSE 332

Page 6: More Graph Algorithms Data Structures and Algorithms

Can we always order a graph?

A graph has a topological ordering if and only if it is a DAG.

A directed graph without any cycles.

Directed Acyclic Graph (DAG)

A

B C

Can you topologically order this graph?

Page 7: More Graph Algorithms Data Structures and Algorithms

Ordering a DAG

Does this graph have a topological ordering? If so find one.

A

B

C

E

D

Page 8: More Graph Algorithms Data Structures and Algorithms

Ordering a DAG

Does this graph have a topological ordering? If so find one.

A

B

C

E

D

If a vertex doesn’t have any edges going into it, we can add it to the

ordering.

More generally, if the only incoming edges are from vertices already in the

ordering, it’s safe to add.

Page 9: More Graph Algorithms Data Structures and Algorithms

How Do We Find a Topological Ordering?

TopologicalSort(Graph G, Vertex source)

count how many incoming edges each vertex has

Collection toProcess = new Collection()

foreach(Vertex v in G){

if(v.edgesRemaining == 0)

toProcess.insert(v)

}

topOrder = new List()

while(toProcess is not empty){

u = toProcess.remove()

topOrder.insert(u)

foreach(edge (u,v) leaving u){

v.edgesRemaining--

if(v.edgesRemaining == 0)

toProcess.insert(v)

}

}

Page 10: More Graph Algorithms Data Structures and Algorithms

What’s the running time?

TopologicalSort(Graph G, Vertex source)

count how many incoming edges each vertex has

Collection toProcess = new Collection()

foreach(Vertex v in G){

if(v.edgesRemaining == 0)

toProcess.insert(v)

}

topOrder = new List()

while(toProcess is not empty){

u = toProcess.remove()

topOrder.insert(u)

foreach(edge (u,v) leaving u){

v.edgesRemaining--

if(v.edgesRemaining == 0)

toProcess.insert(v)

}

}

Running Time: 𝑂( 𝑉 + 𝐸 )

Page 11: More Graph Algorithms Data Structures and Algorithms

Strongly Connected Components

Page 12: More Graph Algorithms Data Structures and Algorithms

Directed Graph Connectedness

D

B C

A E

D

B C

A E

D

B C

A E

Strongly Connected: Can get from

every vertex to every other and

back!

Weakly Connected: Could get from

every vertex to every other and back,

if you ignore the direction on edges.

Disconnected: Can’t get from every

vertex to every other and back, even if

you ignore the direction on edges.

Page 13: More Graph Algorithms Data Structures and Algorithms

Strongly Connected Components

SCCs: {A}, {B,C,D,E}

A set of vertices C such that every pair of vertices in C is connected

via some path in both directions, and there is no other vertex which

is connected to every vertex of C in both directions.

Strongly Connected Component

D

B C

A E

Page 14: More Graph Algorithms Data Structures and Algorithms

Strongly Connected Components Problem

D

C F

B EA K

J

Page 15: More Graph Algorithms Data Structures and Algorithms

Strongly Connected Components Problem

Given: A directed graph G

Find: The strongly connected components of G

Strongly Connected Components Problem

D

C F

B EA K

J

{A}, {B}, {C,D,E,F}, {J,K}

Page 16: More Graph Algorithms Data Structures and Algorithms

SCC Algorithm

Ok. How do we make a computer do this?

You could:

-run a BFS from every vertex,

-For each vertex record what other vertices it can get to

-and figure it out from there.

But you can do better. There’s actually an O(|V|+|E|) algorithm!

Page 17: More Graph Algorithms Data Structures and Algorithms

An Important Subroutine

There’s a second way to traverse a graph.

Depth First Search

-Won’t find you shortest paths

-But does produce interesting information about what vertices you can reach.

Page 18: More Graph Algorithms Data Structures and Algorithms

Depth First Search (DFS)BFS uses a queue to order which vertex we move to next

Gives us a growing “frontier” movement across graph

Can you move in a different pattern? What if you used a stack instead?

dfs(graph)

toVisit.push(first vertex)

mark first vertex as visited

while(toVisit is not empty)

current = toVisit.pop()

for (V : current.neighbors())

if (V is not visited)

toVisit.push(v)

mark v as visited

finished.add(current)

bfs(graph)

toVisit.enqueue(first vertex)

mark first vertex as visited

while(toVisit is not empty)

current = toVisit.dequeue()

for (V : current.neighbors())

if (v is not visited)

toVisit.enqueue(v)

mark v as visited

finished.add(current)

Page 19: More Graph Algorithms Data Structures and Algorithms

Depth First Search

F

B

C

D

A

E

G

H

I

J

Current node:

Stack:

Finished: A B

A

B EC

D

D FG

BE

H

E CFG

I

H

H

I

GFICD

dfs(graph)

toVisit.push(first vertex)

mark first vertex as visited

while(toVisit is not empty)

current = toVisit.pop()

for (V : current.neighbors())

if (V is not visited)

toVisit.push(v)

mark v as visited

finished.add(current)

Page 20: More Graph Algorithms Data Structures and Algorithms

DFS

Running time?

-Same as BFS: 𝑂( 𝑉 + 𝐸 )

You can rewrite DFS to be a recursive method.

Use the call stack as your stack.

No easy trick to do the same with BFS.

Page 21: More Graph Algorithms Data Structures and Algorithms

SCC Algorithm

Ok. How do we make a computer do this?

You could:

-run a BFS from every vertex,

-For each vertex record what other vertices it can get to

-and figure it out from there.

But you can do better. There’s actually an O(|V|+|E|) algorithm!

Page 22: More Graph Algorithms Data Structures and Algorithms

SCC Algorithm

I only want you to remember two things about the algorithm:

-It is an application of depth first search.

-It runs in linear time

The problem with running a [B/D]FS from every vertex is you recomputea lot of information.

The time a vertex is popped off the stack in (recursive) DFS contains a “smart” ordering to do a second DFS where you don’t need to recompute that information.

Page 23: More Graph Algorithms Data Structures and Algorithms

SCC Algorithm

If we run a DFS from A and another one from B, we’ll go through almost the entire graph twice.

Starting at J or K and moving from “right to left” will let us avoid recomputation.

Details at end of this slide deck.

For the final, we might ask you to find Strongly Connected Components, but won’t require you use the algorithm.

D

C F

B EA K

J

Page 24: More Graph Algorithms Data Structures and Algorithms

Why Find SCCs?

Graphs are useful because they encode relationships between arbitrary objects.

Let’s build a new graph out of the SCCs! Call it H

-Have a vertex for each of the strongly connected components

-Add an edge from component 1 to component 2 if there is an edge from a vertex inside 1 to one inside 2.

D

C F

B EA K

J

1

3 4

2

Page 25: More Graph Algorithms Data Structures and Algorithms

Why Find SCCs?

That’s awful meta. Why?

This new graph summarizes reachability information of the original graph.

-I can get from A (of G) in 1 to F (of G) in 3 if and only if I can get from 1to 3 in H.

D

C F

B EA K

J

1

3 4

2

Page 26: More Graph Algorithms Data Structures and Algorithms

Why Must H Be a DAG?

H is always a DAG (do you see why?).

Page 27: More Graph Algorithms Data Structures and Algorithms

Takeaways

Finding SCCs lets you collapse your graph to the meta-structure.If (and only if) your graph is a DAG, you can find a topological sort of your graph.

Both of these algorithms run in linear time.

Just about everything you could want to do with your graph will take at least as long.

You should think of these as “almost free” preprocessing of your graph.

-Your other graph algorithms only need to work on

-topologically sorted graphs and

-strongly connected graphs.

Page 28: More Graph Algorithms Data Structures and Algorithms

Optional Content: Strongly Connected Components Algorithm

Page 29: More Graph Algorithms Data Structures and Algorithms

Efficient SCC

We’d like to find all the vertices in our strongly connected component in time corresponding to the size of the component, not for the whole graph.

We can do that with a DFS (or BFS) as long as we don’t leave our connected component.

If we’re a “sink” component, that’s guaranteed. I.e. a component whose vertex in the meta-graph has no outgoing edges.

How do we find a sink component? We don’t have a meta-graph yet (we need to find the components first)

DFS can find a vertex in a source component, i.e. a component whose vertex in the meta-graph has no incoming edges. - That vertex is the last one to be popped off the stack in the recursive version of DFS.

So if we run DFS in the reversed graph (where each edge points the opposite direction) we can find a sink component.

Page 30: More Graph Algorithms Data Structures and Algorithms

Efficient SCC

So from a DFS in the reversed graph, we can use the order vertices are popped off the stack to find a sink component (in the original graph).

Run a DFS from that vertex to find the vertices in that component in size of that component time.

Now we can delete the edges coming into that component.

The last remaining vertex popped off the stack is a sink of the remaining graph, and now a DFS from them won’t leave the component.

Iterate this process (grab a sink, start DFS, delete edges entering the component).

In total we’ve run two DFSs. (since we never leave our component in the second DFS).

More information, and pseudocode:

https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm

Page 31: More Graph Algorithms Data Structures and Algorithms

Graph Modeling

Page 32: More Graph Algorithms Data Structures and Algorithms

Graph Modeling

Much of the time you don’t need a new graph algorithm.

What you need is to figure out what graph to make and which graph algorithm to run.

“Graph modeling”

Going from word problem to graph algorithm.

Often finding a clever way to turn your requirements into graph features.

Mix of “standard bag of tricks” and new creativity.

Page 33: More Graph Algorithms Data Structures and Algorithms

Graph Modeling Process

1. What are your fundamental objects?

-Those will probably become your vertices.

2. How are those objects related?

-Represent those relationships with edges.

3. How is what I’m looking for encoded in the graph?

-Do I need a path from s to t? The shortest path from s to t? A minimum spanning tree? Something else?

4. Do I know how to find what I’m looking for?

-Then run that algorithm/combination of algorithms

-Otherwise go back to step 1 and try again.

Page 34: More Graph Algorithms Data Structures and Algorithms

Scenario #1

What are the vertices?

What are the edges?

What are we looking for?

What do we run?

You’ve made a new social networking app, Convrs. Users on Convrs can have “asymmetric” following (I can follow you, without you following me). You decide to allow people to form multi-user direct messages, but only if people are probably in similar social circles (to avoid spamming).

You’ll allow a messaging channel to form only if for every pair of users a,b in the channel: a must follow b or follow someone who follows b or follow someone who follows someone who follows b, or …And the same for b to a.

You’d like to be able to quickly check for any new proposed channel whether it meets this condition.

Page 35: More Graph Algorithms Data Structures and Algorithms

Scenario #1You’ve made a new social networking app, Convrs. Users on Convrs can have “asymmetric” following (I can follow you, without you following me). You decide to allow people to form multi-user direct messages, but only if people are probably in similar social circles (to avoid spamming).

You’ll allow a messaging channel to form only if for every pair of users a,b in the channel: a must follow b or follow someone who follows b or follow someone who follows someone who follows b, or …And the same for b to a.

You’d like to be able to quickly check for any new proposed channel whether it meets this condition.

What are the vertices?

What are the edges?

What are we looking for?

What do we run?

Users

Directed – from 𝑢 to 𝑣 if

𝑢 follows 𝑣

If everyone in the channel is

in the same SCC.

Find SCCs, to test a new channel,

make sure all are in same component.

Page 36: More Graph Algorithms Data Structures and Algorithms

Scenario #2

Sports fans often use the “transitive law” to predict sports outcomes -- . In general, if you think A is better than B, and B is also better than C, then you expect that A is better than C.

Teams don’t all play each other – from data of games that have been played, determine if the “transitive law” is realistic, or misleading about at least one outcome.

What are the vertices?

What are the edges?

What are we looking for?

What do we run?

Page 37: More Graph Algorithms Data Structures and Algorithms

Scenario #2

Sports fans often use the “transitive law” to predict sports outcomes -- . In general, if you think A is better than B, and B is also better than C, then you expect that A is better than C.

Teams don’t all play each other – from data of games that have been played, determine if the “transitive law” is realistic, or misleading about at least one outcome.

What are the vertices?

What are the edges?

What are we looking for?

What do we run?

Teams

Directed – Edge from

𝑢 to 𝑣 if 𝑢 beat 𝑣.

A cycle would say it’s not realistic.

OR a topological sort would say it is.

You can modify DFS to find cycles (ask Robbie later).

a topological sort algorithm (with error detection)

Page 38: More Graph Algorithms Data Structures and Algorithms

Scenario #3You are at Splash Mountain. Your best friend is at Space Mountain. You have to tell each other about your experiences in person as soon as possible. Where do you meet and how quickly can you get there?

What are the vertices?

What are the edges?

What are we looking for?

What do we run?

Castle

Flag

Pole

Dumbo

It’s a

small

world

Matter-

horn

Space

Mtn

Star

Tours

Jungle

Cruise

Indiana

Jones

Splash

Mtn

Thunder

Mtn

0

1

2

3

4

5

6 7

8

9

10

11

5

17

13

1210

1

9

6

4

16

7

8

3

2

15

14

0

15

14

29

33

32

19

17

20 37

36

1

36

29

22

19 15

9

17

31

28

0

Page 39: More Graph Algorithms Data Structures and Algorithms

Scenario #3You are at Splash Mountain. Your best friend is at Space Mountain. You have to tell each other about your experiences in person as soon as possible. Where do you meet and how quickly can you get there?

What are the vertices? Rides

What are the edges? Walkways with how long it would take to walk

What are we looking for?- The “midpoint”

What do we run?- Run Dijkstra’s from Splash Mountain, store distances

- Run Dijkstra’s from Space Mountain, store distances

- Iterate over vertices, for each vertex remember max of two

- Iterate over vertices, find minimum of remembered numbers

Castle

Flag

Pole

Dumbo

It’s a

small

world

Matter-

horn

Space

Mtn

Star

Tours

Jungle

Cruise

Indiana

Jones

Splash

Mtn

Thunder

Mtn

0

1

2

3

4

5

6 7

8

9

10

11

5

17

13

1210

1

9

6

4

16

7

8

3

2

15

14

0

15

14

29

33

32

19

17

20 37

36

1

36

29

22

19 15

9

17

31

28

0

Page 40: More Graph Algorithms Data Structures and Algorithms

Scenario #4You’re a Disneyland employee, working the front of the Splash Mountain line. Suddenly, the crowd-control gates fall over and the line degrades into an unordered mass of people.

Sometimes you can tell who was in line before who; for other groups you aren’t quite sure. You need to restore the line, while ensuring if you knew A came before B before the incident, they will still be in the right order afterward.

What are the vertices?

What are the edges?

What are we looking for?

What do we run?

Page 41: More Graph Algorithms Data Structures and Algorithms

Scenario #4You’re a Disneyland employee, working the front of the Splash Mountain line. Suddenly, the crowd-control gates fall over and the line degrades into an unordered mass of people.

Sometimes you can tell who was in line before who; for other groups you aren’t quite sure. You need to restore the line, while ensuring if you knew A came before B before the incident, they will still be in the right order afterward.

What are the vertices? People

What are the edges? Edges are directed, have an edge from X to Y if you know X came before Y.

What are we looking for?- A total ordering consistent with all the ordering we do know.

What do we run?- Topological Sort!