Giter Club home page Giter Club logo

libgraph's Introduction

LibGraph

This is an extensible graph library in C++ with dijkstra routing. It can store any node and edge types that are derived from the Node and Edge classes in this library. They can be mixed in the graph, since it stores them as polymorphic pointers. Just override the Edge::getWeight function in order to return the right weights of your edges, for correct routing with the Dijkstra algorithm.

This work was created based on my C++ classes at the HTW Berlin, where i worked as a lecturer for a few years. Use it as it is without any warranty (MIT license).

How to build

Just build your project with the Graph.cpp, Edge.cpp and Node.cpp and add the corresponding header files. A Makefile to build the files as a static library will be added soon.

Documentation

The functions are documented in the header files.

Example

Here is a simple example how the library could be used:

#include "Graph.h"
#include "SimpleEdge.h"
#include <iostream>

int main()
{
  // You can subclass Node, in order to add functionallity to the nodes.
  Node& rMunich = g.makeNode(Node("Munich"));
  Node& rHamburg = g.makeNode<Node>("Hamburg");
  Node& rBerlin = g.makeNode<Node>("Berlin");
  Node& rFrankfurt = g.makeNode<Node>("Frankfurt");

  // SimpleEdge is useful for some cases, but you can also subclass Edge.
  g.makeEdge<SimpleEdge>(rBerlin, rHamburg, 450);
  g.makeEdge<SimpleEdge>(rHamburg, rBerlin, 450);
  // You can make the edges bidirectional, too:
  g.makeBiEdge<SimpleEdge>(rBerlin, rMunich, 650);
  g.makeBiEdge<SimpleEdge>(rBerlin, rFrankfurt, 590);

  // find the shortest path between any type of nodes, regarding the weight of your edges
  auto path = g.findShortestPathDijkstra(rHamburg, rMunich);
  for (Edge* pEdge : path) {
      // dynamic_cast to your Edge type is useful, if you have multiple different types of edges.
      SimpleEdge* pMyEdge = dynamic_cast<SimpleEdge*>(pEdge);
      if (pMyEdge != NULL) {
          std::cout << pEdge->toString() << std::endl;
      }
  }

  return 0;
}

libgraph's People

Contributors

peters-r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

clyxi joechai93

libgraph's Issues

Graph.h syntax error and illegal qualified name

I just downloaded the library trying to implement it in a project of mine.

I am running into two issues, both in "Graph.h".

Firstly, on line 111, there is an illegal qualified name in member declaration, which can be fixed by deleting the "Graph::" from "Graph::findNodeById".

Secondly, and harder to fix (for me, as I am not an extremely experienced C++ programmer) there is a syntax error in the "Graph& operator << (T& rEdge)" definition, at line 86.

The code reads:
"

// forward as r-value reference
        makeEdge(std::move(rEdge))
        return *this;

"

Which visual studio is suggesting adding a semi-colon at the end of the line containing makeEdge. However, doing that (which I get the feeling is not the correct answer) generates two more issues, at line 74:

"
Error C2440 '': cannot convert from 'initializer list' to 'T'
Error C2672 'Graph::makeEdge': no matching overloaded function found
"

I am building with ISO C++14 Standard and Legacy MSVC C Standard.

Any help on the issue would be greatly appreciated.

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.