mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Refactor Code Style (#4151)
This commit is contained in:
@ -6,7 +6,7 @@ class BellmanFord /*Implementation of Bellman ford to detect negative cycles. Gr
|
||||
start vertex, end vertex and weights. Vertices should be labelled with a number between 0 and total number of vertices-1,both inclusive*/{
|
||||
|
||||
int vertex, edge;
|
||||
private Edge edges[];
|
||||
private Edge[] edges;
|
||||
private int index = 0;
|
||||
|
||||
BellmanFord(int v, int e) {
|
||||
@ -36,7 +36,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
||||
* @param p[] Parent array which shows updates in edges
|
||||
* @param i Current vertex under consideration
|
||||
*/
|
||||
void printPath(int p[], int i) {
|
||||
void printPath(int[] p, int i) {
|
||||
if (p[i] == -1) { // Found the path back to parent
|
||||
return;
|
||||
}
|
||||
@ -44,7 +44,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
public static void main(String[] args) {
|
||||
BellmanFord obj = new BellmanFord(0, 0); // Dummy object to call nonstatic variables
|
||||
obj.go();
|
||||
}
|
||||
@ -55,7 +55,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
||||
System.out.println("Enter no. of vertices and edges please");
|
||||
v = sc.nextInt();
|
||||
e = sc.nextInt();
|
||||
Edge arr[] = new Edge[e]; // Array of edges
|
||||
Edge[] arr = new Edge[e]; // Array of edges
|
||||
System.out.println("Input edges");
|
||||
for (i = 0; i < e; i++) {
|
||||
u = sc.nextInt();
|
||||
@ -63,9 +63,9 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
||||
w = sc.nextInt();
|
||||
arr[i] = new Edge(u, ve, w);
|
||||
}
|
||||
int dist[] = new int[v]; // Distance array for holding the finalized shortest path distance between source
|
||||
int[] dist = new int[v]; // Distance array for holding the finalized shortest path distance between source
|
||||
// and all vertices
|
||||
int p[] = new int[v]; // Parent array for holding the paths
|
||||
int[] p = new int[v]; // Parent array for holding the paths
|
||||
for (i = 0; i < v; i++) {
|
||||
dist[i] = Integer.MAX_VALUE; // Initializing distance values
|
||||
}
|
||||
@ -113,11 +113,11 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
||||
* @param end Ending vertex
|
||||
* @param Edge Array of edges
|
||||
*/
|
||||
public void show(int source, int end, Edge arr[]) { // be created by using addEdge() method and passed by calling getEdgeArray() method // Just shows results of computation, if graph is passed to it. The graph should
|
||||
public void show(int source, int end, Edge[] arr) { // be created by using addEdge() method and passed by calling getEdgeArray() method // Just shows results of computation, if graph is passed to it. The graph should
|
||||
int i, j, v = vertex, e = edge, neg = 0;
|
||||
double dist[] = new double[v]; // Distance array for holding the finalized shortest path distance between source
|
||||
double[] dist = new double[v]; // Distance array for holding the finalized shortest path distance between source
|
||||
// and all vertices
|
||||
int p[] = new int[v]; // Parent array for holding the paths
|
||||
int[] p = new int[v]; // Parent array for holding the paths
|
||||
for (i = 0; i < v; i++) {
|
||||
dist[i] = Integer.MAX_VALUE; // Initializing distance values
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ class dijkstras {
|
||||
|
||||
int k = 9;
|
||||
|
||||
int minDist(int dist[], Boolean Set[]) {
|
||||
int minDist(int[] dist, Boolean[] Set) {
|
||||
int min = Integer.MAX_VALUE, min_index = -1;
|
||||
|
||||
for (int r = 0; r < k; r++) {
|
||||
@ -21,16 +21,16 @@ class dijkstras {
|
||||
return min_index;
|
||||
}
|
||||
|
||||
void print(int dist[]) {
|
||||
void print(int[] dist) {
|
||||
System.out.println("Vertex \t\t Distance");
|
||||
for (int i = 0; i < k; i++) {
|
||||
System.out.println(i + " \t " + dist[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void dijkstra(int graph[][], int src) {
|
||||
int dist[] = new int[k];
|
||||
Boolean Set[] = new Boolean[k];
|
||||
void dijkstra(int[][] graph, int src) {
|
||||
int[] dist = new int[k];
|
||||
Boolean[] Set = new Boolean[k];
|
||||
|
||||
for (int i = 0; i < k; i++) {
|
||||
dist[i] = Integer.MAX_VALUE;
|
||||
@ -60,7 +60,7 @@ class dijkstras {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int graph[][] = new int[][] {
|
||||
int[][] graph = new int[][] {
|
||||
{ 0, 4, 0, 0, 0, 0, 0, 8, 0 },
|
||||
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
|
||||
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
|
||||
|
@ -4,7 +4,7 @@ import java.util.Scanner;
|
||||
|
||||
public class FloydWarshall {
|
||||
|
||||
private int DistanceMatrix[][];
|
||||
private int[][] DistanceMatrix;
|
||||
private int numberofvertices; // number of vertices in the graph
|
||||
public static final int INFINITY = 999;
|
||||
|
||||
@ -15,7 +15,7 @@ public class FloydWarshall {
|
||||
this.numberofvertices = numberofvertices;
|
||||
}
|
||||
|
||||
public void floydwarshall(int AdjacencyMatrix[][]) { // calculates all the distances from source to destination vertex
|
||||
public void floydwarshall(int[][] AdjacencyMatrix) { // calculates all the distances from source to destination vertex
|
||||
for (int source = 1; source <= numberofvertices; source++) {
|
||||
for (
|
||||
int destination = 1;
|
||||
|
@ -122,7 +122,7 @@ class AdjacencyListGraph<E extends Comparable<E>> {
|
||||
|
||||
public class Graphs {
|
||||
|
||||
public static void main(String args[]) {
|
||||
public static void main(String[] args) {
|
||||
AdjacencyListGraph<Integer> graph = new AdjacencyListGraph<>();
|
||||
assert graph.addEdge(1, 2);
|
||||
assert graph.addEdge(1, 5);
|
||||
|
@ -83,7 +83,7 @@ public class Kosaraju {
|
||||
}
|
||||
|
||||
private void sortEdgesByLowestFinishTime(int v, List<List<Integer>> list){
|
||||
int vis[] = new int[v];
|
||||
int[] vis = new int[v];
|
||||
for (int i = 0; i < v; i++) {
|
||||
if(vis[i] == 0){
|
||||
dfs(i, vis, list);
|
||||
@ -110,7 +110,7 @@ public class Kosaraju {
|
||||
* @param transposeGraph Transpose of the given adjacency list
|
||||
*/
|
||||
public void findStronglyConnectedComponents(int v, List<List<Integer>> transposeGraph){
|
||||
int vis[] = new int[v];
|
||||
int[] vis = new int[v];
|
||||
while (!stack.isEmpty()) {
|
||||
var node = stack.pop();
|
||||
if(vis[node] == 0){
|
||||
@ -122,7 +122,7 @@ public class Kosaraju {
|
||||
}
|
||||
|
||||
//Dfs to store the nodes in order of lowest finish time
|
||||
private void dfs(int node, int vis[], List<List<Integer>> list){
|
||||
private void dfs(int node, int[] vis, List<List<Integer>> list){
|
||||
vis[node] = 1;
|
||||
for(Integer neighbour : list.get(node)){
|
||||
if(vis[neighbour] == 0)
|
||||
@ -132,7 +132,7 @@ public class Kosaraju {
|
||||
}
|
||||
|
||||
//Dfs to find all the nodes of each strongly connected component
|
||||
private void dfs2(int node, int vis[], List<List<Integer>> list){
|
||||
private void dfs2(int node, int[] vis, List<List<Integer>> list){
|
||||
vis[node] = 1;
|
||||
for(Integer neighbour : list.get(node)){
|
||||
if(vis[neighbour] == 0)
|
||||
|
@ -14,7 +14,7 @@ import java.util.Queue;
|
||||
*/
|
||||
public class MatrixGraphs {
|
||||
|
||||
public static void main(String args[]) {
|
||||
public static void main(String[] args) {
|
||||
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(10);
|
||||
graph.addEdge(1, 2);
|
||||
graph.addEdge(1, 5);
|
||||
|
@ -12,7 +12,7 @@ class PrimMST {
|
||||
|
||||
// A utility function to find the vertex with minimum key
|
||||
// value, from the set of vertices not yet included in MST
|
||||
int minKey(int key[], Boolean mstSet[]) {
|
||||
int minKey(int[] key, Boolean[] mstSet) {
|
||||
// Initialize min value
|
||||
int min = Integer.MAX_VALUE, min_index = -1;
|
||||
|
||||
@ -28,7 +28,7 @@ class PrimMST {
|
||||
|
||||
// A utility function to print the constructed MST stored in
|
||||
// parent[]
|
||||
void printMST(int parent[], int n, int graph[][]) {
|
||||
void printMST(int[] parent, int n, int[][] graph) {
|
||||
System.out.println("Edge Weight");
|
||||
for (int i = 1; i < V; i++) {
|
||||
System.out.println(
|
||||
@ -39,15 +39,15 @@ class PrimMST {
|
||||
|
||||
// Function to construct and print MST for a graph represented
|
||||
// using adjacency matrix representation
|
||||
void primMST(int graph[][]) {
|
||||
void primMST(int[][] graph) {
|
||||
// Array to store constructed MST
|
||||
int parent[] = new int[V];
|
||||
int[] parent = new int[V];
|
||||
|
||||
// Key values used to pick minimum weight edge in cut
|
||||
int key[] = new int[V];
|
||||
int[] key = new int[V];
|
||||
|
||||
// To represent set of vertices not yet included in MST
|
||||
Boolean mstSet[] = new Boolean[V];
|
||||
Boolean[] mstSet = new Boolean[V];
|
||||
|
||||
// Initialize all keys as INFINITE
|
||||
for (int i = 0; i < V; i++) {
|
||||
@ -103,7 +103,7 @@ class PrimMST {
|
||||
(3)-------(4)
|
||||
9 */
|
||||
PrimMST t = new PrimMST();
|
||||
int graph[][] = new int[][] {
|
||||
int[][] graph = new int[][] {
|
||||
{ 0, 2, 0, 6, 0 },
|
||||
{ 2, 0, 3, 8, 5 },
|
||||
{ 0, 3, 0, 0, 7 },
|
||||
|
@ -68,15 +68,15 @@ public class TarjansAlgorithm {
|
||||
|
||||
// lowTime: indicates the earliest visited vertex (the vertex with minimum insertion time) that can
|
||||
// be reached from a subtree rooted with a particular node.
|
||||
int lowTime[] = new int[V];
|
||||
int insertionTime[] = new int[V];
|
||||
int[] lowTime = new int[V];
|
||||
int[] insertionTime = new int[V];
|
||||
for (int i = 0; i < V; i++) {
|
||||
insertionTime[i] = -1;
|
||||
lowTime[i] = -1;
|
||||
}
|
||||
|
||||
// To check if element is present in stack
|
||||
boolean isInStack[] = new boolean[V];
|
||||
boolean[] isInStack = new boolean[V];
|
||||
|
||||
// Store nodes during DFS
|
||||
Stack<Integer> st = new Stack<Integer>();
|
||||
@ -89,8 +89,8 @@ public class TarjansAlgorithm {
|
||||
return SCClist;
|
||||
}
|
||||
|
||||
private void stronglyConnCompsUtil(int u, int lowTime[], int insertionTime[],
|
||||
boolean isInStack[], Stack<Integer> st, List<List<Integer>> graph) {
|
||||
private void stronglyConnCompsUtil(int u, int[] lowTime, int[] insertionTime,
|
||||
boolean[] isInStack, Stack<Integer> st, List<List<Integer>> graph) {
|
||||
|
||||
// Initialize insertion time and lowTime value of current node
|
||||
insertionTime[u] = Time;
|
||||
@ -101,22 +101,16 @@ public class TarjansAlgorithm {
|
||||
isInStack[u] = true;
|
||||
st.push(u);
|
||||
|
||||
int n;
|
||||
|
||||
// Go through all vertices adjacent to this
|
||||
Iterator<Integer> i = graph.get(u).iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
n = i.next();
|
||||
|
||||
for (Integer vertex : graph.get(u)) {
|
||||
//If the adjacent node is unvisited, do DFS
|
||||
if (insertionTime[n] == -1) {
|
||||
stronglyConnCompsUtil(n, lowTime, insertionTime, isInStack, st, graph);
|
||||
if (insertionTime[vertex] == -1) {
|
||||
stronglyConnCompsUtil(vertex, lowTime, insertionTime, isInStack, st, graph);
|
||||
//update lowTime for the current node comparing lowtime of adj node
|
||||
lowTime[u] = Math.min(lowTime[u], lowTime[n]);
|
||||
} else if (isInStack[n]) {
|
||||
lowTime[u] = Math.min(lowTime[u], lowTime[vertex]);
|
||||
} else if (isInStack[vertex]) {
|
||||
//If adj node is in stack, update low
|
||||
lowTime[u] = Math.min(lowTime[u], insertionTime[n]);
|
||||
lowTime[u] = Math.min(lowTime[u], insertionTime[vertex]);
|
||||
}
|
||||
}
|
||||
//If lowtime and insertion time are same, current node is the head of an SCC
|
||||
|
Reference in New Issue
Block a user