Skip to content
Snippets Groups Projects
Commit dc56b06b authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

added implementation of undirected graph

parent 570d460e
No related branches found
No related tags found
No related merge requests found
package io.ad.graphs;
import java.util.*;
/*
*
*/
public class Graph {
// Kante
static class Edge {
Node dest; // Zielknoten
int cost; // Kantengewicht
public Edge(Node n, int c) {
dest = n;
cost = c;
}
public Node getDestNode() {
return dest;
}
public int getCost() {
return cost;
}
}
// Knoten
static class Node {
String label;
ArrayList<Edge> adjList = new ArrayList<Edge>();
public Node(String s) {
label = s;
}
public String toString() {
return label;
}
public String getLabel() {
return label;
}
public void addEdge(Edge e) {
adjList.add(e);
}
public Iterator<Edge> getEdges() {
return adjList.iterator();
}
public Edge getEdgeTo(Node n) {
for (Edge e : adjList) {
if (e.dest.equals(n))
return e;
}
return null;
}
}
// Verzeichnis aller Knoten des Graphen
private HashMap<String, Node> nodeSet = new HashMap<String, Node>();
public Graph() {
}
public Node addNode(String label) throws RuntimeException {
if (nodeSet.containsKey(label))
throw new RuntimeException("Node Already Defined!");
Node n = new Node(label);
nodeSet.put(label, n);
return n;
}
public Node getNode(String label) throws NoSuchElementException {
Node n = nodeSet.get(label);
if (n == null)
throw new NoSuchElementException();
return n;
}
public void addEdge(String src, String dest, int cost) {
Node srcNode = getNode(src);
Node destNode = getNode(dest);
srcNode.addEdge(new Edge(destNode, cost));
//Make Graph undirected!
destNode.addEdge(new Edge(srcNode, cost));
}
/* TODO: Homework
* Realize a method that checks whether two nodes are connected.
*/
public boolean checkConnection(String node1, String node2) {
if (node1==null || node2==null) throw new RuntimeException();
return nodeSet.get(node1).getEdgeTo(nodeSet.get(node2))!=null;
}
/* TODO: Homework
* Realize a method that returns a list of neighbor-nodes.
* Get the adjList of node. Iterate over his Edges and
* get the label of each destination node. Store labels
* in a new List of Strings.
*/
public List<String> getNeighbours(String label) {
if (label==null) throw new RuntimeException();
Node node = nodeSet.get(label);
List<String> neighbors = new LinkedList<>();
Iterator<Edge> it = node.getEdges();
while(it.hasNext()) {
neighbors.add(it.next().getDestNode().getLabel());
}
return neighbors;
}
/*TODO: Homework
* Realize breadth-first search (BFS) using a the java.util.Queue-Interface
* and an appropriate implementation.
*
*/
public void bfs(String start) {
//TODO Validieriung von start
Node startNode = nodeSet.get(start);
Queue<Node> queue = new LinkedList<>();
Map<Node, Boolean> visited = new HashMap<>();
nodeSet.values().forEach(node -> {visited.put(node, false);});
queue.add(startNode);
visited.put(startNode, true);
while(!queue.isEmpty()) {
Node node = queue.remove();
System.out.println(node.getLabel());
List<String> neighbors = getNeighbours(node.getLabel());
for (String neighbor : neighbors) {
if (!visited.get(getNode(neighbor))) {
visited.put(getNode(neighbor), true);
queue.add(getNode(neighbor));
}
}
}
}
/*TODO: Homework
* Realize an iterative variant of depth-first search (DFS) using a LinkedList as a Stack.
*
*/
public void dfs(String start) {
Node startNode = nodeSet.get(start);
LinkedList<Node> stack = new LinkedList<>();
Map<Node, Boolean> visited = new HashMap<>();
nodeSet.values().forEach(node -> {visited.put(node, false);});
stack.add(0,startNode); // push on stack
visited.put(startNode, true);
while(!stack.isEmpty()) {
Node node = stack.remove(0); // pop from stack
System.out.println(node.getLabel());
List<String> neighbors = getNeighbours(node.getLabel());
for (String neighbor : neighbors) {
if (!visited.get(getNode(neighbor))) {
visited.put(getNode(neighbor), true);
stack.add(0,getNode(neighbor)); //push on stack
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment