GithubHelp home page GithubHelp logo

graphs's Introduction

Graphs

This repo contains implementations of following algorithms:

  • RB Tree
  • Kruskal's algorithm
  • Dijkstra’s algorithm

Consider each separately:

RB Tree

RBNode

public class RBNode extends Node 

public methods:

1.  public RBNode(Integer key, Object data, RBNode leftChild, RBNode rightChild, RBNode parent, boolean color, Boolean nil)

constructor

2. public String toStringWithChildren()

returns a tree converted to a string below this node:

RBTree

public class RBTree

public methods:

1. public RBNode getRoot()

returns root node of tree and null if tree is empty

2. public boolean addElement(RBNode node)

returns true if element was added and false if tree contains node with similar key

3. public boolean deleteElement(Integer key)

returns true if element with given key was deleted and false if element with such key doesn't exist

4. public RBNode search(Integer key)

returns node with given key or null if node with given key doesn't exist

5. public RBNode search(Integer key, RBNode startNode)

returns node with given key which is below startNode

6. public Integer getDepth()

returns depth of tree

7. public void print()

prints tree in the same way as RBNode.toStringWithChildren():

8. public boolean testIsOK()

checks the tree for violations of red-black tree rules:

  • every node is red or black, the root node is black,
  • no two red nodes appear consecutively,
  • the path from the root node to all four null leaf nodes passes through two black nodes (either 2 and then 1 , or 2 and then 3) on the way to the null leaves.
9. public Integer getBlackDepth(RBNode node)

returns black depth of the tree

Kruskal's algorithm & Dijkstra’s algorithm

The graph is stored as a list of edges.

public class Edge implements Comparable<Edge>{
    private Node startNode;
    private Node endNode;
    private Integer mass;
    public Integer getMass();
    public void setMass(Integer mass) ;
    public Node getEndNode();
    public void setEndNode(Node endNode);
    public Node getStartNode();
    public void setStartNode(Node startNode);
}
    
public class Node {
    private Integer key;
    private Object data;
    public int wayWeight = Integer.MAX_VALUE;
    public int[] way;
    public int wayLength;
    public void setData(Object data)
    public Object getData()
    public void setKey(Integer key)
    public Integer getKey()
}

Graph

public class Graph

public methods

1. public void init()

Creates local arrays. Must be called if you don't set them by hands:

    public ArrayList<Node> getNodes();
    public void setNodes(ArrayList<Node> nodes);
    public ArrayList<Edge> getEdges();
    public void setEdges(ArrayList<Edge> edges);
2. public Node getNode(Integer key);

returns node by given key

3. public boolean isInitialized();

returns true if local arrays were initialized or set and false if they are nulls

4. 
    public boolean addEdge(Edge edge);
    public boolean addNode(Node node);
    public boolean removeEdge(Edge edge);
    public boolean removeNode(Node node);
    public boolean removeNode(Integer key);

returns true if action was done and false if not

5.     
    public ArrayList<Edge> getEdgesFromNode(Node node)
    public ArrayList<Edge> getEdgesFromNode(Integer key)

returns edges coming out of the node

6.
    public ArrayList<Edge> getEdgesToNode(Node node)
    public ArrayList<Edge> getEdgesToNode(Integer key)

returns edges incoming in the node

7. public ArrayList<Edge> getEdges(Integer key);

returns all edges connected to the node

8. public Graph getMinSpanningTree();

returns minimal spanning tree using Kruskal's algorithm

9. public boolean equalsUndirected(Graph graph);

compares an undirected graph with a given one. Returns true if the keys at their nodes and the edges connecting the nodes are equal

10. public void printEdgesUndirected();

prints edges of graph without direction

11. public void findMinWays(Node rootNode);

finds shortest ways using Dijkstra’s algorithm. Results are put in nodes

12. public ArrayList<Node> getNeighbours(Node node);

returns neighbours of given node

13. public void printWays();

allows to print found ways after calling findMinWays(Node rootNode)

misc for testing:

    1. public Graph readAnswerForKraskalFrom(String str);

builds graf using following format:

numberOfNodes
key11 key12 massOfEdge1
key21 key22 massOfEdge2
 ...   ...     ...

where keys of nodes are equal to their index number.

    2. public ArrayList<Node> readAnswerForDijkstraFrom(String str);

builds nodes using following format:

key1 weightOfWay1
key2 weightOfWay2
...      ...

graphs's People

Contributors

ilkoch008 avatar

Stargazers

 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.