Giter Site home page Giter Site logo

sleekpanther / topological-ordering Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 267 KB

Finds a Topological Ordering of vertices in a Directed Acyclic Graph

Java 100.00%
noah patullo noah-patullo noahpatullo patulo pattulo pattullo topological-ordering topological-order topological

topological-ordering's Introduction

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<ArrayList<Integer>> graph1 = new ArrayList<ArrayList<Integer>>();
    • 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<Integer>());
  • 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

topological-ordering's People

Contributors

sleekpanther avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.