An open API service indexing awesome lists of open source software.

https://github.com/sleekpanther/topological-ordering

Finds a Topological Ordering of vertices in a Directed Acyclic Graph
https://github.com/sleekpanther/topological-ordering

algorithm-design algorithms graph graph-algorithms graphs incoming incoming-edge noah noah-patullo noahpatullo order pattullo pattulo patullo patulo sort topological topological-order topological-ordering topological-sort

Last synced: 4 months ago
JSON representation

Finds a Topological Ordering of vertices in a Directed Acyclic Graph

Awesome Lists containing this project

README

        

# Topological Ordering
Finds a Topological Ordering of vertices in a Directed Acyclic Graph

## Problem Statement
**Topological Ordering:** arranges nodes as v1, v2 ..., vn so that for every edge (vi, vj), i < j
- Must be a Directed Graph
- Must NOT contain a cycle

### Graph 1

### Graph 1 Topological Ordering

**Can can be more than 1 topologcal ordering. Algorithm finds 1 if 1 exists**

## Algorithm
A DAG must have a node with **no incoming edges**
Pick a node `v` with no incoming edges
Print `v`
Calculate a topological ordering on G – {`v`}
Ends when all nodes are included in the ordering

Keep track of 2 things:
- `count[w]` = number of incoming edges
- `S` = set of nodes with no incoming edges

Set-Up
- Scan through the graph to initialize `count[]` and `S`
- **O(m + n)**

Finding the Ordering
- Remove v from S
- Decrement `count[w]` for all edges from `v` to` w`
add `w` to `S` if `count[w]` hits `0`
- **O(1) per edge**

## Runtime
**O(m + n)**

## Usage
**Detects cycles if graph is not a DAG and prints an error message**
**Node names are integers for simplicity of the code & start at `0`**

- Create a graph as an adjacency list
`ArrayList> graph1 = new ArrayList>();`
- Add rows for each vertex. `graph1.get(u)` is a list of nodes representing edges FROM `u`
- Orphan nodes are allowed (i.e. a node with no incoming or outgoing edges). It doesn't have to be the last node in the graph, but is probably easier if it is. Just make sure no edges go to that node. (e.g. if node `5` is an orphan, then `5` must not appear in any of the rows of the adjacency list)
- Nodes with no outgoing edges just need an empty `ArrayList`
`graph1.add(new ArrayList());`
- Run `TopologicalOrdering.findTopologicalOrdering(graph1);`

### Graph 2

### Graph 2 Topological Ordering

### Graph 3 (No topological ordering)

Contains a cycle, & no nodes with no incoming edges

### Graph 4 (No topological ordering)

Contains a cycle

## References
- [Kevin Wayne Slides](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/03Graphs.pdf#page=45)
- [University of Washington slides](https://homes.cs.washington.edu/~jrl/421slides/lec5.pdf)
- [Rajesh Rao Slides](https://courses.cs.washington.edu/courses/cse326/03wi/lectures/RaoLect20.pdf)