*(Guest post by Michael Krzeminski)*

Lecture 19

Today we started by briefly reviewing the **Breadth First Search **algorithm and its O(m+n) Implementation

L[i] is the ith layer CC[u] = True (True if u ε V has already been explored)

1) CC[s] = True & CC[u] = False ∀ u ≠ s

2)i set to 0

3)L[i] = {s}

4)While(L[i] ≠ Ø)

For every u ε L[i]

For every (u, v) ε E

If CC[v] = False then

Add v to L[i+1]

CC[v] = True

We then saw how we could make the algorithm more efficient by treating all the layers as one large layer and using a queue instead of a linked list.

The professor then briefly went over how we could replace the queue with a stack to implement the **Depth First Search** algorithm in O(m+n) time.

The professor then went over **Directed Graphs**

• They model asymmetric relationships

• Are used to represent precedence relationships (u needs to be done before v mean an edge from u to v)

• The adjacency matric is not symmetric (there could be a 1 in the u column for v but a 0 in the v column for u)

He then introduced **Directed Acyclic Graphs**

• DAGs are simply Directed Graphs that lack directed cycles

• They are a cousin of trees, though they are able to represent more information due to the fact that for n vertices DAGs have edges while trees only have n-1 edges.

The he showed us **Topological Sorting of a DAG**

** ** •an ordering of the vertices of a graph such that all the edges go “forward”

•an application is to order jobs that are dependent on each other such that they can be completed without waiting

He defined a **Topolocial Sorting** by its input/output

**Input:** Directed Acyclic Graph G = (V, E)

**Output:** Ordering on V say v_{1}, v_{2}, … , v_{n} s.t. (v_{i} , v_{j }) ε E → i < j

A dumb Algorithm for this sorting would be to go through all n! orderings and check if any satisfy, which would be bounded by Ω(n!).

He then went on to start proving that a graph being a DAG is equivalent to that that DAG has a valid topological sorting. To prove equivalence he would need to prove a → b and b → a. So he started by showing if G has a topological sorting then G is a DAG and he did this by contradiction.

**Proof**: Assume G has a topological sorting but isn’t a DAG meaning that it has a cycle.

→ → →

v_{1}, v_{2}, … , v_{n }

Obs. All edges in a t.s. go forward.

Obs. A cycle needs to have a backwards edge to be able to return to the original vertices.

This contradicts the definition of a topological sorting thus proving, that is a graph G has a topological sorting then it is a DAG.

He then made two additional observations that will help us with the second half of the proof next class. That v_{1} cannot have an incoming edge and that there is a always a vertex that has all outgoing edges.

## Leave a Reply