Giter Club home page Giter Club logo

homework-graphslibrary's Introduction

Overview

A simple Java project to implement the Graphs data structure as a reusable library.

Use

To create a new Graph, follow these steps

1. Create a new Graph

Every Graph will start out completely empty. You will new need to add vertices and edges.

import com.peterlibs.graphs;

Graph myNewGraph = new Graph();

2. Add Vertices

Every Vertex in the Graph must be uniquely named. If no name for the Vertex is provided, the default String "label" will be used

myNewGraph.addVertex("vertex-1");
myNewGraph.addVertex("vertex-2");
myNewGraph.addVertex("vertex-3");
myNewGraph.addVertex("vertex-4");

3. Add Edges

Edges in this Graph are directional and weighted. The default weight is 0. When adding an Edge, you must specify the label of the starting Vertex and ending Vertex. If the Vertex does not exist, an Exception will be thrown

myNewGraph.addEdge("vertex-1", "vertex-2");
myNewGraph.addEdge("vertex-2", "vertex-3", 10);
myNewGraph.addEdge("vertex-3", "vertex-4", 20);

To update an Edge's weight, simply re-add the Edge.

myNewGraph.addEdge("vertex-1", "vertex-2", 30);
myNewGraph.addEdge("vertex-2", "vertex-3", 50);

4. Get Paths (all, shortest, or longest)

With your Graph built, you can now ask it to get the path(s) between two vertices. Each possible path with be returned as a com.peterlibs.graph.Path object. This object will contain a list of the com.peterlibs.graph.Vertex objects that define the path. You can also get the path as a list of com.peterlibs.graph.Edge objects. The Path also has a method for easily getting the "total cost" of this path.

Path shortestPath = myNewGraph.findShortestPath("vertex-1", "vertex-3");
List<Vertex> thePath = shortestPath.getVertices();
int totalCost = shortestPath.getCost();

Testing

Unit tests have been written to test the functionality of the methods in the provided classes. The following classes have complete unit tests

  • com.peterlibs.graph.Graph
  • com.peterlibs.graph.Vertex
  • com.peterlibs.graph.Edge
  • com.peterlibs.graph.Path

When testing the Graph class, the following graphs were used. Dense Graph Sparse Graph

homework-graphslibrary's People

Contributors

pwalker91 avatar

Stargazers

 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.