Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.
47 changes: 47 additions & 0 deletions dijkstra-algorithm/src/main/java/com/bobocode/GraphImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,70 @@

import java.util.Set;

/**
* {@link GraphImpl} implements Graph {@link Graph}, using nodes as vertices. Nodes are stores in instances of nested
* class Node. The graph can be initialized from a file, or by calling methods to add a node {@link GraphImpl#addNode(String)}
* and add an edge {@link GraphImpl#addDestination(String, String, int)}
*
*/
public class GraphImpl implements Graph {

/**
* This method creates a Graph from file
*
* File format e.g.
* nodeA nodeB nodeC
* nodeA nodeB 1
* nodeA nodeC 12
* @param fileName file contains graph struct
* @return a new graph contains vertices and edges
*/
@Override
public Graph fromFile(String fileName) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
}

/**
* Adds a node to the graph, if it doesn't exist
*
* @param nodeName name of node to add
*/
@Override
public void addNode(String nodeName) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
}

/**
* Adds a edge to the graph between nodes
*
* @param nodeFromName name of first node in edge
* @param nodeToName name of second node in edge
* @param distance the weight of edge
*/
@Override
public void addDestination(String nodeFromName, String nodeToName, int distance) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
}

/**
* Calculates shortest distance between two nodes
*
* @param nodeStart name of node start path
* @param nodeEnd name of node finish path
* @return min distance between two nodes
*/
@Override
public int calculateShortestDistance(String nodeStart, String nodeEnd) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
}

/**
* Calculates shortest path between two nodes
*
* @param nodeStart name of node start path
* @param nodeEnd name of node finish path
* @return set of nodes, contains min path between two nodes
*/
@Override
public Set<String> calculateShortestPath(String nodeStart, String nodeEnd) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
package com.bobocode.interfaces;

import java.util.Set;

/**
* Graph is a structure amounting to a set of objects in which some pairs of the objects are in some sense "related".
*/
public interface Graph {
/**
* Adds new vertices in graph.
*
* @param nodeName the name of node to add
*/
void addNode(String nodeName);

/**
* Adds edge between two vertices.
*
* @param nodeFromName the name of first node in edge
* @param nodeToName the name of second node in edge
* @param distance distance between vertices
*/
void addDestination(String nodeFromName, String nodeToName, int distance);

/**
* Returns shortest distance between two vertices
*
* @param nodeStart the name of start node
* @param nodeEnd the name of finish node
* @return an integer value that is distance between vertices
*/
int calculateShortestDistance(String nodeStart, String nodeEnd);

/**
* Returns shortest path between two vertices
*
* @param nodeStart the name of start node
* @param nodeEnd the name of finish node
* @return a set that contains node names for shortest path between vertices
*/
Set<String> calculateShortestPath(String nodeStart, String nodeEnd);

/**
* Returns configured Graph from file
*
* File format e.g.
* nodeA nodeB nodeC
*
* nodeA nodeB 1
* nodeA nodeC 12
*
* @param fileName the name of file, which contains graph
* @return a graph that contains vertices from file
*/
Graph fromFile(String fileName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,12 @@ public void testIncorrectDistanceIntoGraph() {
fail("Exception must be IncorrectDistanceException");
}
}

@Test
public void testCorrectCalculateGraphFromFile() {
graph = graph.fromFile("graph.txt");

assertEquals(graph.calculateShortestDistance("nodeA", "nodeG"), 19);
assertEquals(graph.calculateShortestPath("nodeA", "nodeC"), Set.of("nodeA", "nodeC", "nodeE", "nodeG"));
}
}
12 changes: 12 additions & 0 deletions linked-queue/src/test/resources/graph.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
nodeA nodeB nodeC nodeD nodeE nodeF nodeG
nodeA nodeB 10
nodeA nodeC 8
nodeB nodeD 3
nodeC nodeE 8
nodeC nodeF 16
nodeC nodeG 25
nodeD nodeE 5
nodeD nodeF 3
nodeE nodeF 2
nodeE nodeG 3
nodeF nodeG 6