Code Monkey home page Code Monkey logo

graph-strong-connectivity's Introduction

Strong Connectivity

Application of Breadth-First Search to see if a directed graph is Strongly Connected

Problem Statement

  • Vertex u and v are mutually reachable if there is a path from u to v and also a path from v to u
  • A directed graph is strongly connected if every pair of nodes is mutually reachable

Algorithm

  • Run BFS from any vertex
  • Make Grev, a new graph with edge directions reversed
  • Run BFS on Grev
  • Return true iff every vertex is reachable in both BFS on G and BFS on Grev

Input Graphs

Graph 1 (is stronly connected)

Graph 1 Reversed


Graph 2 (NOT stronly connected)

Graph 2 Reversed

Usage

  • Make a new StrongConnectivity() object
  • Create a graph as an ArrayList<ArrayList<Integer>> Adjacency List
    • Each graph.get(u) is an ArrayList<Integer> of vertices representing edges going From u
    • Vertex names are simply increasing integer values, no need for human-readable vertex ID's like S or T
    • Graph adjacency list must have all edges represented
    • If there are no outgoing edges from a vertex, add an empty ArrayList
      graph2.add(new ArrayList<Integer>());
  • Call isStronglyConnected() and pass in a graph
    boolean isGraph1StronglyConnected = strongConnectivityFinder.isStronglyConnected(graph1);

Code Notes

  • isStronglyConnected() always chooses vertex 0 to start BFS, but that's completely arbitrary

References

graph-strong-connectivity'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.