CS130-lecture-20201130

Exercise solns #

IMAGE IMAGE IMAGE

Graph cont. #

IMAGE

Gets shortest path because it starts by searching all edges that are 1 away, then 2 away, and so forth.

Tracing the BFS implementation

IMAGE IMAGE IMAGE

Enter the loop

IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE

Path tree on right ^

IMAGE IMAGE

Performance:

  • Worst case runtime is O(E) or O(V+E)
  • Worst case space complexity O(V), when every vertice is added to the queue

IMAGE IMAGE IMAGE IMAGE

  1. There is a maximum of (V(V-1)) vertices in a digraph with no parallel or self-loops. “Each edge can connect to every other edge.”
  2. The minimum number of edges is (V-1).

IMAGE IMAGE

  1. The indegree for 6 is 2
  2. The outdegree for 6 is 1

IMAGE IMAGE

Space complexity of reverse is O(V+E). Runtime complexity is O(V+E).

IMAGE

What vertices are reachable from

  • Source 1: 1
  • Source 2: 0, 1, 2, 3, 4, 5
  • Source 1,2,6: All except 7 and 8

IMAGE IMAGE

Directing cycle

IMAGE

This would be impossible to take any courses. There cannot be any directed cycles in the digraph, it would be impossible to find the topological order. (This is a DAG).

IMAGE IMAGE

Topological sort with DFS trace

IMAGE IMAGE

Vertexes in different color have been visited

IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE

0, 3, 7, 6, 5 are reachable from 0. Now go by vertex order and check 1

IMAGE IMAGE IMAGE

Now check 2

IMAGE IMAGE

Everything has been visited now. Our reversePost is finished. All edges flow from left to right.

IMAGE

IMAGE

The runtime for the constructor is O(V+E). Space complexity for the constructor is O(V).

IMAGE

0: 6
1: 11
2: 0, 3
3: 6, 10
4: 1
5: 2, 10
6: 2
7: 8, 11
8: 1, 4
9:
10: 3
11: 8

Screen Shot 2020-12-01 at 1.55.45 PM.png

IMAGE