Git Product home page Git Product logo

graph-algorithms's Introduction

Graph Algorithms

Build status Coverage Status PRs Welcome Project Status: Active – The project has reached a stable, usable state and is being actively developed. license-badge

For more information please refer to the pharo-ai wiki: https://github.com/pharo-ai/wiki/blob/master/wiki/Graphs/Graph-Algorithms.md

Or also to our graphs booklet Booklet-PharoGraphs

Table of Contents

Description

This library contains several graphs algorithms. The nodes in the graph can be any kind of object: a Character, a String, an Integer or a complex object.

How to install it

Metacello new
    repository: 'github://pharo-ai/graph-algorithms';
    baseline: 'AIGraphAlgorithms';
    load

How to depend on it

If you want to add this repo to your Metacello Baselines or Configurations, copy and paste the following expression:

spec
    baseline: 'AIGraphAlgorithms' 
    with: [ spec repository: 'github://pharo-ai/graph-algorithms' ]

Implemented graph algorithms

  • Tarjan’s Algorithm: Strongly Connected Components
  • BFS: Breath First Search
  • Graph Reducer: Merge all strongly connected components in a graph to a single node
  • Dijkstra: Shortest path in a weighted graph
  • Bellman-Ford: Shortest path in negative weighted graphs
  • Kruskal: Minimum or Maximum expanding tree in a graph
  • HITS: Hyperlink-Induced Topic Search
  • Topological Sort
  • Shortest Path in DAG
  • Longest path in DAG
  • Longest path in any type of graph
  • Dinic: strongly polynomial algorithm for computing the maximum flow in a flow network
  • A* algorithm: searching algorithm for having the shortest path

How to use the graph algorithms

The below code was extracted from the Pharo Graphs booklet which is a booklet in which this library along with all the algorithms are explained. You can check it out in Booklet-PharoGraphs

All the graph algorithms of this library share a common API also. The class AIGraphAlgorithm provides the common API to add nodes, edges, searching the nodes, etc.

Some of the common methods are:

  • algorithm nodes:
  • algorithm nodes
  • algorithm edges
  • algorithm edges:from:to:
  • algorithm edges:from:to:weight:
  • algorithm findNode:
  • algorithm run

Example 1

For using the topological sort algorithm, we can run this code snippet

"First define the nodes and the edges"
nodes := #( $A $B $C $D $E $F $G ).
edges := #( #( $A $B ) #( $A $C ) #( $B $E ) #( $C $E ) #( $C $D )
    #( $D $E ) #( $D $F ) #( $E $G ) #( $F $G ) ).

"Instantiate the graph algorithm"
topSortingAlgo := AITopologicalSorting new.

"Set the nodes and edges"    
topSortingAlgo nodes: nodes.
topSortingAlgo
    edges: edges
    from: [ :each | each first ]
    to: [ :each | each second ].

"Run to obtain the result"
topologicalSortedElements := topSortingAlgo run.

Example 2

Or if we want to find the shortest path in a weighted graph:

nodes := $A to: $F.
edges := #( #( $A $B 5 ) #( $A $C 1 ) #( $B $C 2 ) #( $B $E 20 )
    #( $B $D 3 ) #( $C $B 3 ) #( $C $E 12 ) #( $D $C 3 )
    #( $D $E 2 ) #( $D $F 6 ) #( $E $F 1 ) ).

dijkstra := AIDijkstra new.
dijkstra nodes: nodes.
dijkstra
    edges: edges
    from: [ :each | each first ]
    to: [ :each | each second ]
    weight: [ :each | each third ].

shortestPathAToB := dijkstra runFrom: $A to: $B.
pathDistanceAToB := (dijkstra findNode: $B) pathDistance.

dijkstra end: $F.
shortestPathAToF := dijkstra reconstructPath.
pathDistanceAToF := (dijkstra findNode: $F) pathDistance.

dijkstra reset.
shortestPathBToE := dijkstra runFrom: $B to: $E.

Graph generation algorithms

This library also contains algorithms for generating regular and random graphs. This algorithms are not loaded by default. To load them, you can either load them manually using Iceberg directly from the Pharo image or load the GraphGenerators baseline group.

The algorithms implemented are:

  • Albert Barabasi Graph Generator
  • Atlas Graph Graph Generator
  • Erdos Renyi GNM Graph Generator
  • Erdos Renyi GNP Graph Generator
  • Grid 2D Graph Generator
  • Grid 3D Graph Generator
  • Hexagonal Lattice Graph Generator
  • Kleinberg Graph Generator
  • Triangular Lattice Graph Generator
  • Waltz Strogatz Graph Generator

graph-algorithms's People

Contributors

jordanmontt avatar virenvarma007 avatar sanjaybhat2004 avatar astares avatar jecisc avatar sergestinckwich avatar privat avatar stephaneggermont avatar ducasse avatar

Watchers

 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.