DFS Topological Sort and Shorstest Path Problem Flashcards

1
Q

Topological Sort

A
  • Topological sort
  • DFS can be used to perform a topological sort of a directed acyclic graph
  • A topological sort of a DAG is a linear ordering of all its vertices such that if DAG contains an edge (u, v), then u appears before v in ordering
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Topological Sort Wotk

A

• Don’t need to sort by finish times

– Can just output vertices as they are finished and understand that we want the reverse of this list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Shortest path problem examples

A
  • Airlineflighttimes
  • Telephonecommunicationcosts
  • Computernetworksresponsetimes • Fastestwaytogettoschoolbycar
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Single-source shortest path

A

Find a shortest path from a given source (vertex s) to each of the vertices. Dijkstra’s algorithm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Single-pair shortest path

A

Given two vertices, find a shortest path between them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

All-pairs shortest path

A

Find shortest-paths for every pair of vertices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Shortest path problem definition #1

A

• Definition 1: Given a path p=v1 →v2 →…→vk , the weight of the path is the sum of the weights of all its edges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Shortest path problem definition #2

A

• Definition 2: The shortest path weight from u to v is defined
by:

δ(u,v)=% {()$min w p : p path from u to v if there is a path from u to v

∞ otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Shortest path problem definition #3

A

A shortest path from vertex u to vertex v is defined as any path p with weight w(p)= δ(u,v)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dijkstra’s algorithm

A

Assumptions:
– Directed or undirected graph
– All edges must have nonnegative weights
– Connected graph

• Input: Weighted graph G=(E,V) and source vertex v∈V

•Output: Lengths of shortest paths from a given source
vertex v ∈ V to all other vertices

With slight modification we can obtain the path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Dijkstra’s algorithm running time

A

|V|Time-extract-min + |E|Time-Decrease-key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Greedy Algorithm

A

•Dijkstra’s algorithm uses a greedy strategy for designing algorithms

• Greedy approach
– In order to get what you want just start grabbing what looks best

  • Shortest path we wish to find the path with lowest weight
  • It always chooses the lightest vertex
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Topological Sort Algorithm

A

TOPOLOGICAL-SORT(G)
call DFS(G) to compute finishing time for each vertex v
as each vertex is finished, insert it onto the front of a linked list
return linked list

Can just output vertices as they are finished and understand that we want the reverse of this list

• Space:O(V+E) • Time:O(V+E)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly