diff --git a/src/main/java/experiments/autofix/classes/AutofixClass_1.java b/src/main/java/experiments/autofix/classes/AutofixClass_1.java new file mode 100644 index 0000000..635f8ad --- /dev/null +++ b/src/main/java/experiments/autofix/classes/AutofixClass_1.java @@ -0,0 +1,36 @@ + + +package experiments.autofix.classes; + +import java.util.*; + +public class AutofixClass_1 { + public static int[][] AutofixMethod_2(int[][] g, int n, int m, int[] verticesToConnect) { + for (int k = 0; k < n; k++) + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + int[][] dp = new int[1 << m][n]; + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + dp[1 << i][j] = g[verticesToConnect[i]][j]; + return dp; + } + public static void AutofixMethod_1(int[][] dp, int m, int n, int[][] g) { + for (int i = 1; i < 1 << m; i++) { + if (((i - 1) & i) != 0) { + for (int j = 0; j < n; j++) { + dp[i][j] = Integer.MAX_VALUE / 2; + for (int k = (i - 1) & i; k > 0; k = (k - 1) & i) { + dp[i][j] = Math.min(dp[i][j], dp[k][j] + dp[i ^ k][j]); + } + } + for (int j = 0; j < n; j++) { + for (int k = 0; k < n; k++) { + dp[i][j] = Math.min(dp[i][j], dp[i][k] + g[k][j]); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/experiments/package1/Biconnectivity.java b/src/main/java/experiments/package1/Biconnectivity.java index 6a2fb99..df2dd51 100644 --- a/src/main/java/experiments/package1/Biconnectivity.java +++ b/src/main/java/experiments/package1/Biconnectivity.java @@ -1,10 +1,12 @@ + + package experiments.package1; import java.util.*; + import java.util.stream.Stream; public class Biconnectivity { - List[] graph; boolean[] visited; Stack stack; @@ -14,7 +16,6 @@ public class Biconnectivity { List> edgeBiconnectedComponents; List cutPoints; List bridges; - public List> biconnectivity(List[] graph) { int n = graph.length; this.graph = graph; @@ -26,14 +27,11 @@ public List> biconnectivity(List[] graph) { edgeBiconnectedComponents = new ArrayList<>(); cutPoints = new ArrayList<>(); bridges = new ArrayList<>(); - for (int u = 0; u < n; u++) if (!visited[u]) dfs(u, -1); - return edgeBiconnectedComponents; } - void dfs(int u, int p) { visited[u] = true; lowlink[u] = tin[u] = time++; @@ -44,12 +42,12 @@ void dfs(int u, int p) { if (v == p) continue; if (visited[v]) { - lowlink[u] = Math.min(lowlink[u], tin[v]); // or lowlink[u] = Math.min(lowlink[u], lowlink[v]); + lowlink[u] = Math.min(lowlink[u], tin[v]); } else { dfs(v, u); lowlink[u] = Math.min(lowlink[u], lowlink[v]); - cutPoint |= tin[u] <= lowlink[v]; - if (tin[u] < lowlink[v]) // or if (lowlink[v] == tin[v]) + cutPoint |= tin[u] <= lowlink[v]; + if (tin[u] < lowlink[v]) bridges.add("(" + u + "," + v + ")"); ++children; } @@ -69,7 +67,6 @@ void dfs(int u, int p) { edgeBiconnectedComponents.add(component); } } - public static List[] ebcTree(List[] graph, List> components) { int[] comp = new int[graph.length]; for (int i = 0; i < components.size(); i++) @@ -82,19 +79,15 @@ public static List[] ebcTree(List[] graph, List> g[comp[u]].add(comp[v]); return g; } - public static void main(String[] args) { List[] graph = Stream.generate(ArrayList::new).limit(6).toArray(List[]::new); - int[][] esges = {{0, 1}, {1, 2}, {0, 2}, {2, 3}, {1, 4}, {4, 5}, {5, 1}}; for (int[] edge : esges) { graph[edge[0]].add(edge[1]); graph[edge[1]].add(edge[0]); } - Biconnectivity bc = new Biconnectivity(); List> components = bc.biconnectivity(graph); - System.out.println("edge-biconnected components:" + components); System.out.println("cut points: " + bc.cutPoints); System.out.println("bridges:" + bc.bridges); diff --git a/src/main/java/experiments/package1/Graph.java b/src/main/java/experiments/package1/Graph.java index 7070503..3184989 100644 --- a/src/main/java/experiments/package1/Graph.java +++ b/src/main/java/experiments/package1/Graph.java @@ -1,163 +1,144 @@ + + package experiments.package1; import java.util.ArrayList; + import java.util.Arrays; + import java.util.LinkedList; + import java.util.List; + import java.util.Queue; + import java.util.Scanner; class Edge { - private final int initialVertex; - private final int terminalVertex; - - public Edge(int initialVertex, int terminalVertex) { - this.initialVertex = initialVertex; - this.terminalVertex = terminalVertex; - } - - public int getInitialVertex() { - return initialVertex; - } - public int getTerminalVertex() { - return terminalVertex; - } + private final int initialVertex; + private final int terminalVertex; + public Edge(int initialVertex, int terminalVertex) { + this.initialVertex = initialVertex; + this.terminalVertex = terminalVertex; + } + public int getInitialVertex() { + return initialVertex; + } + public int getTerminalVertex() { + return terminalVertex; + } } -/** - * Graph - */ -public class Graph { - - private final boolean isDirectedGraph; - private final int numOfNodes; - private final int numOfEdges; - private boolean[] visited; - private List[] adjacencyList; - - @SuppressWarnings("unchecked") - public Graph(int numOfNodes, int numOfEdges, boolean isDirectedGraph, List edges) { - this.numOfNodes = numOfNodes; - this.numOfEdges = numOfEdges; - this.isDirectedGraph = isDirectedGraph; - this.visited = new boolean[numOfNodes + 1]; - this.adjacencyList = new ArrayList[numOfNodes + 1]; - for(int i = 0; i <= numOfNodes; i++) { - this.adjacencyList[i] = new ArrayList(); - } - - if (isDirectedGraph) { - for(Edge edge : edges) { - addDirectedEdge(edge); - } - } - else { - for(Edge edge : edges) { - addUndirectedEdge(edge); - } - } - } - - public boolean isDirected() { - return isDirectedGraph; - } - - public int getNumOfNodes() { - return numOfNodes; - } - - public int getNumOfEdges() { - return numOfEdges; - } - private void addDirectedEdge(Edge edge) { - adjacencyList[edge.getInitialVertex()].add(edge.getTerminalVertex()); - } - - private void addUndirectedEdge(Edge edge) { - adjacencyList[edge.getInitialVertex()].add(edge.getTerminalVertex()); - adjacencyList[edge.getTerminalVertex()].add(edge.getInitialVertex()); - } - - private void resetVisited() { - Arrays.fill(visited, false); - } - - public static int readNumOfNodes(Scanner sc) { - System.out.print("Enter number of nodes : "); - int numOfNodes = sc.nextInt(); - return numOfNodes; - } - - public static int readNumOfEdges(Scanner sc) { - System.out.print("Enter number of edges : "); - int numOfEdges = sc.nextInt(); - return numOfEdges; - } - - public static List readEdges(int numOfEdges, Scanner sc) { - int i, initialVertex, terminalVertex; - List edges = new ArrayList<>(); - System.out.println("Enter edges :"); - for(i = 0; i < numOfEdges; i++) { - initialVertex = sc.nextInt(); - terminalVertex = sc.nextInt(); - edges.add(new Edge(initialVertex, terminalVertex)); - } - - return edges; - } - - private void exploreNode(int nodeNum) { - visited[nodeNum] = true; - System.out.printf("%d ", nodeNum); - - for(int neighbour : adjacencyList[nodeNum]) { - if (visited[neighbour] == false) { - exploreNode(neighbour); - } - } - } - - public void runDFS() { - for(int nodeNum = 1; nodeNum <= numOfNodes; nodeNum++) { - if (visited[nodeNum] == false) { - exploreNode(nodeNum); - } - } - resetVisited(); - System.out.println(); - } - - public void runBFS(int startNode) { - Queue queue = new LinkedList<>(); - queue.add(startNode); - visited[startNode] = true; - - int currentNode; - - while(!queue.isEmpty()) { - currentNode = queue.remove(); - System.out.printf("%d ", currentNode); - for(int neighbour : adjacencyList[currentNode]) { - if (visited[neighbour] == false) { - visited[neighbour] = true; - queue.add(neighbour); - } - } - } - System.out.println(); - resetVisited(); - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int numOfNodes = Graph.readNumOfNodes(sc); - int numOfEdges = Graph.readNumOfEdges(sc); - List edges = Graph.readEdges(numOfEdges, sc); - sc.close(); - Graph graph = new Graph(numOfNodes, numOfEdges, false, edges); - - graph.runDFS(); - graph.runBFS(1); - } +public class Graph { + private final boolean isDirectedGraph; + private final int numOfNodes; + private final int numOfEdges; + private boolean[] visited; + private List[] adjacencyList; + @SuppressWarnings("unchecked") + public Graph(int numOfNodes, int numOfEdges, boolean isDirectedGraph, List edges) { + this.numOfNodes = numOfNodes; + this.numOfEdges = numOfEdges; + this.isDirectedGraph = isDirectedGraph; + this.visited = new boolean[numOfNodes + 1]; + this.adjacencyList = new ArrayList[numOfNodes + 1]; + for (int i = 0; i <= numOfNodes; i++) { + this.adjacencyList[i] = new ArrayList(); + } + if (isDirectedGraph) { + for (Edge edge : edges) { + addDirectedEdge(edge); + } + } else { + for (Edge edge : edges) { + addUndirectedEdge(edge); + } + } + } + public boolean isDirected() { + return isDirectedGraph; + } + public int getNumOfNodes() { + return numOfNodes; + } + public int getNumOfEdges() { + return numOfEdges; + } + private void addDirectedEdge(Edge edge) { + adjacencyList[edge.getInitialVertex()].add(edge.getTerminalVertex()); + } + private void addUndirectedEdge(Edge edge) { + adjacencyList[edge.getInitialVertex()].add(edge.getTerminalVertex()); + adjacencyList[edge.getTerminalVertex()].add(edge.getInitialVertex()); + } + private void resetVisited() { + Arrays.fill(visited, false); + } + public static int readNumOfNodes(Scanner sc) { + System.out.print("Enter number of nodes : "); + int numOfNodes = sc.nextInt(); + return numOfNodes; + } + public static int readNumOfEdges(Scanner sc) { + System.out.print("Enter number of edges : "); + int numOfEdges = sc.nextInt(); + return numOfEdges; + } + public static List readEdges(int numOfEdges, Scanner sc) { + int i, initialVertex, terminalVertex; + List edges = new ArrayList<>(); + System.out.println("Enter edges :"); + for (i = 0; i < numOfEdges; i++) { + initialVertex = sc.nextInt(); + terminalVertex = sc.nextInt(); + edges.add(new Edge(initialVertex, terminalVertex)); + } + return edges; + } + private void exploreNode(int nodeNum) { + visited[nodeNum] = true; + System.out.printf("%d ", nodeNum); + for (int neighbour : adjacencyList[nodeNum]) { + if (visited[neighbour] == false) { + exploreNode(neighbour); + } + } + } + public void runDFS() { + for (int nodeNum = 1; nodeNum <= numOfNodes; nodeNum++) { + if (visited[nodeNum] == false) { + exploreNode(nodeNum); + } + } + resetVisited(); + System.out.println(); + } + public void runBFS(int startNode) { + Queue queue = new LinkedList<>(); + queue.add(startNode); + visited[startNode] = true; + int currentNode; + while (!queue.isEmpty()) { + currentNode = queue.remove(); + System.out.printf("%d ", currentNode); + for (int neighbour : adjacencyList[currentNode]) { + if (visited[neighbour] == false) { + visited[neighbour] = true; + queue.add(neighbour); + } + } + } + System.out.println(); + resetVisited(); + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int numOfNodes = Graph.readNumOfNodes(sc); + int numOfEdges = Graph.readNumOfEdges(sc); + List edges = Graph.readEdges(numOfEdges, sc); + sc.close(); + Graph graph = new Graph(numOfNodes, numOfEdges, false, edges); + graph.runDFS(); + graph.runBFS(1); + } } \ No newline at end of file diff --git a/src/main/java/experiments/package1/SteinerTree.java b/src/main/java/experiments/package1/SteinerTree.java index e1c7521..543ab5d 100644 --- a/src/main/java/experiments/package1/SteinerTree.java +++ b/src/main/java/experiments/package1/SteinerTree.java @@ -1,42 +1,18 @@ + + package experiments.package1; -public class SteinerTree { +import experiments.autofix.classes.AutofixClass_1; +public class SteinerTree { public static int minLengthSteinerTree(int[][] g, int[] verticesToConnect) { int n = g.length; int m = verticesToConnect.length; if (m <= 1) return 0; - - System.out.println("Very important operation being performed here"); - - for (int k = 0; k < n; k++) - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); - - int[][] dp = new int[1 << m][n]; - - for (int i = 0; i < m; i++) - for (int j = 0; j < n; j++) - dp[1 << i][j] = g[verticesToConnect[i]][j]; - - for (int i = 1; i < 1 << m; i++) { - if (((i - 1) & i) != 0) { - for (int j = 0; j < n; j++) { - dp[i][j] = Integer.MAX_VALUE / 2; - for (int k = (i - 1) & i; k > 0; k = (k - 1) & i) { - dp[i][j] = Math.min(dp[i][j], dp[k][j] + dp[i ^ k][j]); - } - } - for (int j = 0; j < n; j++) { - for (int k = 0; k < n; k++) { - dp[i][j] = Math.min(dp[i][j], dp[i][k] + g[k][j]); - } - } - } - } - + System.out.println("Very important operation being performed here"); + int[][] dp = AutofixClass_1.AutofixMethod_2(g, n, m, verticesToConnect); + AutofixClass_1.AutofixMethod_1(dp, m, n, g); return dp[(1 << m) - 1][verticesToConnect[0]]; } -} \ No newline at end of file +} diff --git a/src/main/java/experiments/package2/Biconnectivity.java b/src/main/java/experiments/package2/Biconnectivity.java index d8464d7..5f8bcbf 100644 --- a/src/main/java/experiments/package2/Biconnectivity.java +++ b/src/main/java/experiments/package2/Biconnectivity.java @@ -1,10 +1,12 @@ + + package experiments.package2; import java.util.*; + import java.util.stream.Stream; public class Biconnectivity { - List[] bicongraph; boolean[] isVisited; Stack theStack; @@ -14,7 +16,6 @@ public class Biconnectivity { List> edgeBiconComps; List theCutPoints; List theBridges; - public List> biconnectivity(List[] graph) { int n = graph.length; this.bicongraph = graph; @@ -26,14 +27,11 @@ public List> biconnectivity(List[] graph) { edgeBiconComps = new ArrayList<>(); theCutPoints = new ArrayList<>(); theBridges = new ArrayList<>(); - for (int u = 0; u < n; u++) if (!isVisited[u]) dfs(u, -1); - return edgeBiconComps; } - void dfs(int u, int p) { isVisited[u] = true; lowLinkData[u] = timeOfIn[u] = timer++; @@ -44,20 +42,20 @@ void dfs(int u, int p) { if (v == p) continue; if (isVisited[v]) { - lowLinkData[u] = Math.min(lowLinkData[u], timeOfIn[v]); // or lowlink[u] = Math.min(lowlink[u], lowlink[v]); + lowLinkData[u] = Math.min(lowLinkData[u], timeOfIn[v]); } else { dfs(v, u); lowLinkData[u] = Math.min(lowLinkData[u], lowLinkData[v]); - isCutPoint |= timeOfIn[u] <= lowLinkData[v]; - if (timeOfIn[u] < lowLinkData[v]) // or if (lowlink[v] == tin[v]) + isCutPoint |= timeOfIn[u] <= lowLinkData[v]; + if (timeOfIn[u] < lowLinkData[v]) theBridges.add("(" + u + "," + v + ")"); ++childrenCount; } - } + } if (p == -1) - isCutPoint = childrenCount >= 2; - System.out.println(childrenCount); - System.out.println(isCutPoint); + isCutPoint = childrenCount >= 2; + System.out.println(childrenCount); + System.out.println(isCutPoint); if (isCutPoint) theCutPoints.add(u); if (timeOfIn[u] == lowLinkData[u]) { @@ -71,7 +69,6 @@ void dfs(int u, int p) { edgeBiconComps.add(theComponent); } } - public static List[] ebcTree(List[] graph, List> components) { int[] comp = new int[graph.length]; for (int i = 0; i < components.size(); i++) @@ -84,19 +81,15 @@ public static List[] ebcTree(List[] graph, List> g[comp[u]].add(comp[v]); return g; } - public static void main(String[] args) { List[] graph = Stream.generate(ArrayList::new).limit(6).toArray(List[]::new); - int[][] esges = {{0, 1}, {1, 2}, {0, 2}, {2, 3}, {1, 4}, {4, 5}, {5, 1}}; for (int[] edge : esges) { graph[edge[0]].add(edge[1]); graph[edge[1]].add(edge[0]); } - Biconnectivity bc = new Biconnectivity(); List> components = bc.biconnectivity(graph); - System.out.println("edge-biconnected components:" + components); System.out.println("cut points: " + bc.theCutPoints); System.out.println("bridges:" + bc.theBridges); diff --git a/src/main/java/experiments/package2/Graph.java b/src/main/java/experiments/package2/Graph.java index 4784418..e2940cc 100644 --- a/src/main/java/experiments/package2/Graph.java +++ b/src/main/java/experiments/package2/Graph.java @@ -1,163 +1,144 @@ + + package experiments.package2; import java.util.ArrayList; + import java.util.Arrays; + import java.util.LinkedList; + import java.util.List; + import java.util.Queue; + import java.util.Scanner; class Edge { - private final int startVertex; - private final int endVertex; - - public Edge(int initialVertex, int terminalVertex) { - this.startVertex = initialVertex; - this.endVertex = terminalVertex; - } - - public int getInitialVertex() { - return startVertex; - } - public int getTerminalVertex() { - return endVertex; - } + private final int startVertex; + private final int endVertex; + public Edge(int initialVertex, int terminalVertex) { + this.startVertex = initialVertex; + this.endVertex = terminalVertex; + } + public int getInitialVertex() { + return startVertex; + } + public int getTerminalVertex() { + return endVertex; + } } -/** - * Graph - */ -public class Graph { - - private final boolean isDG; - private final int nodeCount; - private final int edgeCount; - private boolean[] isVisited; - private List[] adjList; - - @SuppressWarnings("unchecked") - public Graph(int numOfNodes, int numOfEdges, boolean isDirectedGraph, List edges) { - this.nodeCount = numOfNodes; - this.edgeCount = numOfEdges; - this.isDG = isDirectedGraph; - this.isVisited = new boolean[numOfNodes + 1]; - this.adjList = new ArrayList[numOfNodes + 1]; - for(int i = 0; i <= numOfNodes; i++) { - this.adjList[i] = new ArrayList(); - } - - if (isDirectedGraph) { - for(Edge edge : edges) { - addDirectedEdge(edge); - } - } - else { - for(Edge edge : edges) { - addUndirectedEdge(edge); - } - } - } - - public boolean isDirected() { - return isDG; - } - - public int getNumOfNodes() { - return nodeCount; - } - - public int getNumOfEdges() { - return edgeCount; - } - private void addDirectedEdge(Edge edge) { - adjList[edge.getInitialVertex()].add(edge.getTerminalVertex()); - } - - private void addUndirectedEdge(Edge edge) { - adjList[edge.getInitialVertex()].add(edge.getTerminalVertex()); - adjList[edge.getTerminalVertex()].add(edge.getInitialVertex()); - } - - private void resetVisited() { - Arrays.fill(isVisited, false); - } - - public static int readNumOfNodes(Scanner scanner) { - System.out.print("Enter number of nodes : "); - int numOfNodes = scanner.nextInt(); - return numOfNodes; - } - - public static int readNumOfEdges(Scanner scanner) { - System.out.print("Enter number of edges : "); - int numOfEdges = scanner.nextInt(); - return numOfEdges; - } - - public static List readEdges(int edgeCount, Scanner scanner) { - int i, startVertex, endVertex; - List edges = new ArrayList<>(); - System.out.println("Enter edges :"); - for(i = 0; i < edgeCount; i++) { - startVertex = scanner.nextInt(); - endVertex = scanner.nextInt(); - edges.add(new Edge(startVertex, endVertex)); - } - - return edges; - } - - private void exploreNode(int nodeNum) { - isVisited[nodeNum] = true; - System.out.printf("%d ", nodeNum); - - for(int adjNode : adjList[nodeNum]) { - if (isVisited[adjNode] == false) { - exploreNode(adjNode); - } - } - } - - public void runDFS() { - for(int nodeNum = 1; nodeNum <= nodeCount; nodeNum++) { - if (isVisited[nodeNum] == false) { - exploreNode(nodeNum); - } - } - resetVisited(); - System.out.println(); - } - - public void runBFS(int startNode) { - Queue queue = new LinkedList<>(); - queue.add(startNode); - isVisited[startNode] = true; - - int currentNode; - - while(!queue.isEmpty()) { - currentNode = queue.remove(); - System.out.printf("%d ", currentNode); - for(int neighbour : adjList[currentNode]) { - if (isVisited[neighbour] == false) { - isVisited[neighbour] = true; - queue.add(neighbour); - } - } - } - System.out.println(); - resetVisited(); - } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int nodeCount = Graph.readNumOfNodes(scanner); - int edgeCount = Graph.readNumOfEdges(scanner); - List edges = Graph.readEdges(edgeCount, scanner); - scanner.close(); - Graph dupGraph = new Graph(nodeCount, edgeCount, false, edges); - - dupGraph.runDFS(); - dupGraph.runBFS(1); - } +public class Graph { + private final boolean isDG; + private final int nodeCount; + private final int edgeCount; + private boolean[] isVisited; + private List[] adjList; + @SuppressWarnings("unchecked") + public Graph(int numOfNodes, int numOfEdges, boolean isDirectedGraph, List edges) { + this.nodeCount = numOfNodes; + this.edgeCount = numOfEdges; + this.isDG = isDirectedGraph; + this.isVisited = new boolean[numOfNodes + 1]; + this.adjList = new ArrayList[numOfNodes + 1]; + for (int i = 0; i <= numOfNodes; i++) { + this.adjList[i] = new ArrayList(); + } + if (isDirectedGraph) { + for (Edge edge : edges) { + addDirectedEdge(edge); + } + } else { + for (Edge edge : edges) { + addUndirectedEdge(edge); + } + } + } + public boolean isDirected() { + return isDG; + } + public int getNumOfNodes() { + return nodeCount; + } + public int getNumOfEdges() { + return edgeCount; + } + private void addDirectedEdge(Edge edge) { + adjList[edge.getInitialVertex()].add(edge.getTerminalVertex()); + } + private void addUndirectedEdge(Edge edge) { + adjList[edge.getInitialVertex()].add(edge.getTerminalVertex()); + adjList[edge.getTerminalVertex()].add(edge.getInitialVertex()); + } + private void resetVisited() { + Arrays.fill(isVisited, false); + } + public static int readNumOfNodes(Scanner scanner) { + System.out.print("Enter number of nodes : "); + int numOfNodes = scanner.nextInt(); + return numOfNodes; + } + public static int readNumOfEdges(Scanner scanner) { + System.out.print("Enter number of edges : "); + int numOfEdges = scanner.nextInt(); + return numOfEdges; + } + public static List readEdges(int edgeCount, Scanner scanner) { + int i, startVertex, endVertex; + List edges = new ArrayList<>(); + System.out.println("Enter edges :"); + for (i = 0; i < edgeCount; i++) { + startVertex = scanner.nextInt(); + endVertex = scanner.nextInt(); + edges.add(new Edge(startVertex, endVertex)); + } + return edges; + } + private void exploreNode(int nodeNum) { + isVisited[nodeNum] = true; + System.out.printf("%d ", nodeNum); + for (int adjNode : adjList[nodeNum]) { + if (isVisited[adjNode] == false) { + exploreNode(adjNode); + } + } + } + public void runDFS() { + for (int nodeNum = 1; nodeNum <= nodeCount; nodeNum++) { + if (isVisited[nodeNum] == false) { + exploreNode(nodeNum); + } + } + resetVisited(); + System.out.println(); + } + public void runBFS(int startNode) { + Queue queue = new LinkedList<>(); + queue.add(startNode); + isVisited[startNode] = true; + int currentNode; + while (!queue.isEmpty()) { + currentNode = queue.remove(); + System.out.printf("%d ", currentNode); + for (int neighbour : adjList[currentNode]) { + if (isVisited[neighbour] == false) { + isVisited[neighbour] = true; + queue.add(neighbour); + } + } + } + System.out.println(); + resetVisited(); + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int nodeCount = Graph.readNumOfNodes(scanner); + int edgeCount = Graph.readNumOfEdges(scanner); + List edges = Graph.readEdges(edgeCount, scanner); + scanner.close(); + Graph dupGraph = new Graph(nodeCount, edgeCount, false, edges); + dupGraph.runDFS(); + dupGraph.runBFS(1); + } } \ No newline at end of file diff --git a/src/main/java/experiments/package2/SteinerTree.java b/src/main/java/experiments/package2/SteinerTree.java index c68ca67..0274d78 100644 --- a/src/main/java/experiments/package2/SteinerTree.java +++ b/src/main/java/experiments/package2/SteinerTree.java @@ -1,43 +1,19 @@ + + package experiments.package2; -public class SteinerTree { +import experiments.autofix.classes.AutofixClass_1; +public class SteinerTree { public static int minLengthSteinerTree(int[][] g, int[] verticesToConnect) { int n = g.length; int m = verticesToConnect.length; if (m <= 1) return 0; - - for (int k = 0; k < n; k++) - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); - - int[][] dp = new int[1 << m][n]; - - for (int i = 0; i < m; i++) - for (int j = 0; j < n; j++) - dp[1 << i][j] = g[verticesToConnect[i]][j]; - - System.out.println("Random junk being printed"); - System.out.println("Codegraph test"); - - for (int i = 1; i < 1 << m; i++) { - if (((i - 1) & i) != 0) { - for (int j = 0; j < n; j++) { - dp[i][j] = Integer.MAX_VALUE / 2; - for (int k = (i - 1) & i; k > 0; k = (k - 1) & i) { - dp[i][j] = Math.min(dp[i][j], dp[k][j] + dp[i ^ k][j]); - } - } - for (int j = 0; j < n; j++) { - for (int k = 0; k < n; k++) { - dp[i][j] = Math.min(dp[i][j], dp[i][k] + g[k][j]); - } - } - } - } - + int[][] dp = AutofixClass_1.AutofixMethod_2(g, n, m, verticesToConnect); + System.out.println("Random junk being printed"); + System.out.println("Codegraph test"); + AutofixClass_1.AutofixMethod_1(dp, m, n, g); return dp[(1 << m) - 1][verticesToConnect[0]]; } } \ No newline at end of file