From 05a9bb1c9c08968435a4142371106d9a18c5ed68 Mon Sep 17 00:00:00 2001 From: Febaug <33418541+Febaug@users.noreply.github.com> Date: Fri, 17 Nov 2017 16:37:13 +0100 Subject: [PATCH 1/2] Add files via upload --- .../Graphs/ConnectedComponent.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Data Structures/Graphs/ConnectedComponent.java diff --git a/Data Structures/Graphs/ConnectedComponent.java b/Data Structures/Graphs/ConnectedComponent.java new file mode 100644 index 00000000..d5bc560e --- /dev/null +++ b/Data Structures/Graphs/ConnectedComponent.java @@ -0,0 +1,150 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +/** + * A class that counts the number of connected components in a graph + * + * @author Lukas Keul + * + */ +class Graph> { + + class Node { + E name; + + public Node(E name) { + this.name = name; + } + } + + class Edge { + Node startNode, endNode; + + public Edge(Node startNode, Node endNode) { + this.startNode = startNode; + this.endNode = endNode; + } + } + + ArrayList edgeList; + ArrayList nodeList; + + public Graph() { + edgeList = new ArrayList(); + nodeList = new ArrayList(); + } + + /** + * Adds a new Edge to the graph. If the nodes aren't yet in nodeList, they + * will be added to it. + * + * @param startNode + * the starting Node from the edge + * + * @param endNode + * the ending Node from the edge + */ + public void addEdge(E startNode, E endNode) { + Node start = null, end = null; + for (Node node : nodeList) { + if (startNode.compareTo(node.name) == 0) { + start = node; + } + else if (endNode.compareTo(node.name) == 0) { + end = node; + } + } + if (start == null) { + start = new Node(startNode); + nodeList.add(start); + } + if (end == null) { + end = new Node(endNode); + nodeList.add(end); + } + + edgeList.add(new Edge(start, end)); + } + + /** + * Main method used for counting the connected components. Iterates through + * the array of nodes to do a depth first search to get all nodes of the + * graph from the actual node. These nodes are added to the array + * markedNodes and will be ignored if they are chosen in the nodeList. + * + * @return returns the amount of unconnected graphs + * + */ + public int countGraphs() { + int count = 0; + Set markedNodes = new HashSet(); + + for (Node n : nodeList) { + if (!markedNodes.contains(n)) { + markedNodes.add(n); + markedNodes.addAll(depthFirstSearch(n, new ArrayList())); + count++; + } + } + + return count; + } + + /** + * Implementation of depth first search. + * + * @param n + * the actual visiting node + * + * @param visited + * A list of already visited nodes in the depth first search + * + * @return returns a set of visited nodes + * + */ + public ArrayList depthFirstSearch(Node n, ArrayList visited) { + visited.add(n); + for (Edge e : edgeList) { + if (e.startNode.equals(n) && !visited.contains(e.endNode)) { + depthFirstSearch(e.endNode, visited); + } + } + return visited; + } +} + +public class ConnectedComponent { + + public static void main(String[] args) { + Graph graphChars = new Graph(); + + // Graph 1 + graphChars.addEdge('a', 'b'); + graphChars.addEdge('a', 'e'); + graphChars.addEdge('b', 'e'); + graphChars.addEdge('b', 'c'); + graphChars.addEdge('c', 'd'); + graphChars.addEdge('d', 'a'); + + graphChars.addEdge('x', 'y'); + graphChars.addEdge('x', 'z'); + + graphChars.addEdge('w', 'w'); + + Graph graphInts = new Graph(); + + // Graph 2 + graphInts.addEdge(1, 2); + graphInts.addEdge(2, 3); + graphInts.addEdge(2, 4); + graphInts.addEdge(3, 5); + + graphInts.addEdge(7, 8); + graphInts.addEdge(8, 10); + graphInts.addEdge(10, 8); + + System.out.println("Amount of different char-graphs: " + graphChars.countGraphs()); + System.out.println("Amount of different int-graphs: " + graphInts.countGraphs()); + } +} From 075b30f2438eb3d9635e6ae2746845fd08c31de2 Mon Sep 17 00:00:00 2001 From: Febaug <33418541+Febaug@users.noreply.github.com> Date: Fri, 17 Nov 2017 16:44:18 +0100 Subject: [PATCH 2/2] Update ConnectedComponent.java --- Data Structures/Graphs/ConnectedComponent.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data Structures/Graphs/ConnectedComponent.java b/Data Structures/Graphs/ConnectedComponent.java index d5bc560e..779d7a84 100644 --- a/Data Structures/Graphs/ConnectedComponent.java +++ b/Data Structures/Graphs/ConnectedComponent.java @@ -3,9 +3,9 @@ import java.util.HashSet; import java.util.Set; /** - * A class that counts the number of connected components in a graph + * A class that counts the number of different connected components in a graph * - * @author Lukas Keul + * @author Lukas Keul, Florian Mercks * */ class Graph> {