mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 05:22:39 +08:00
Close Scanner
to avoid resource leak (#5077)
This commit is contained in:
@ -2,9 +2,13 @@ package com.thealgorithms.datastructures.graphs;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class BellmanFord /*Implementation of Bellman ford to detect negative cycles. Graph accepts inputs
|
||||
in form of edges which have start vertex, end vertex and weights. Vertices should be labelled with a
|
||||
number between 0 and total number of vertices-1,both inclusive*/
|
||||
class BellmanFord /*
|
||||
* Implementation of Bellman ford to detect negative cycles. Graph accepts
|
||||
* inputs
|
||||
* in form of edges which have 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;
|
||||
@ -36,7 +40,7 @@ number between 0 and total number of vertices-1,both inclusive*/
|
||||
|
||||
/**
|
||||
* @param p[] Parent array which shows updates in edges
|
||||
* @param i Current vertex under consideration
|
||||
* @param i Current vertex under consideration
|
||||
*/
|
||||
void printPath(int[] p, int i) {
|
||||
if (p[i] == -1) { // Found the path back to parent
|
||||
@ -52,64 +56,65 @@ number between 0 and total number of vertices-1,both inclusive*/
|
||||
}
|
||||
|
||||
public void go() { // shows distance to all vertices // Interactive run for understanding the
|
||||
// class first time. Assumes source vertex is 0 and
|
||||
Scanner sc = new Scanner(System.in); // Grab scanner object for user input
|
||||
int i, v, e, u, ve, w, j, neg = 0;
|
||||
System.out.println("Enter no. of vertices and edges please");
|
||||
v = sc.nextInt();
|
||||
e = sc.nextInt();
|
||||
Edge[] arr = new Edge[e]; // Array of edges
|
||||
System.out.println("Input edges");
|
||||
for (i = 0; i < e; i++) {
|
||||
u = sc.nextInt();
|
||||
ve = sc.nextInt();
|
||||
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
|
||||
// and all vertices
|
||||
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
|
||||
}
|
||||
dist[0] = 0;
|
||||
p[0] = -1;
|
||||
for (i = 0; i < v - 1; i++) {
|
||||
for (j = 0; j < e; j++) {
|
||||
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
|
||||
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
|
||||
p[arr[j].v] = arr[j].u;
|
||||
try ( // class first time. Assumes source vertex is 0 and
|
||||
Scanner sc = new Scanner(System.in)) {
|
||||
int i, v, e, u, ve, w, j, neg = 0;
|
||||
System.out.println("Enter no. of vertices and edges please");
|
||||
v = sc.nextInt();
|
||||
e = sc.nextInt();
|
||||
Edge[] arr = new Edge[e]; // Array of edges
|
||||
System.out.println("Input edges");
|
||||
for (i = 0; i < e; i++) {
|
||||
u = sc.nextInt();
|
||||
ve = sc.nextInt();
|
||||
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
|
||||
// and all vertices
|
||||
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
|
||||
}
|
||||
dist[0] = 0;
|
||||
p[0] = -1;
|
||||
for (i = 0; i < v - 1; i++) {
|
||||
for (j = 0; j < e; j++) {
|
||||
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
|
||||
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
|
||||
p[arr[j].v] = arr[j].u;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Final cycle for negative checking
|
||||
for (j = 0; j < e; j++) {
|
||||
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
|
||||
neg = 1;
|
||||
System.out.println("Negative cycle");
|
||||
break;
|
||||
// Final cycle for negative checking
|
||||
for (j = 0; j < e; j++) {
|
||||
if (dist[arr[j].u] != Integer.MAX_VALUE && dist[arr[j].v] > dist[arr[j].u] + arr[j].w) {
|
||||
neg = 1;
|
||||
System.out.println("Negative cycle");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (neg == 0) { // Go ahead and show results of computation
|
||||
System.out.println("Distances are: ");
|
||||
for (i = 0; i < v; i++) {
|
||||
System.out.println(i + " " + dist[i]);
|
||||
}
|
||||
System.out.println("Path followed:");
|
||||
for (i = 0; i < v; i++) {
|
||||
System.out.print("0 ");
|
||||
printPath(p, i);
|
||||
System.out.println();
|
||||
if (neg == 0) { // Go ahead and show results of computation
|
||||
System.out.println("Distances are: ");
|
||||
for (i = 0; i < v; i++) {
|
||||
System.out.println(i + " " + dist[i]);
|
||||
}
|
||||
System.out.println("Path followed:");
|
||||
for (i = 0; i < v; i++) {
|
||||
System.out.print("0 ");
|
||||
printPath(p, i);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param source Starting vertex
|
||||
* @param end Ending vertex
|
||||
* @param Edge Array of edges
|
||||
* @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()
|
||||
|
@ -11,21 +11,22 @@ import java.util.Stack;
|
||||
public class ReverseStack {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter the number of elements you wish to insert in the stack");
|
||||
int n = sc.nextInt();
|
||||
int i;
|
||||
Stack<Integer> stack = new Stack<Integer>();
|
||||
System.out.println("Enter the stack elements");
|
||||
for (i = 0; i < n; i++) {
|
||||
stack.push(sc.nextInt());
|
||||
}
|
||||
sc.close();
|
||||
reverseStack(stack);
|
||||
System.out.println("The reversed stack is:");
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.print(stack.peek() + ",");
|
||||
stack.pop();
|
||||
try (Scanner sc = new Scanner(System.in)) {
|
||||
System.out.println("Enter the number of elements you wish to insert in the stack");
|
||||
int n = sc.nextInt();
|
||||
int i;
|
||||
Stack<Integer> stack = new Stack<Integer>();
|
||||
System.out.println("Enter the stack elements");
|
||||
for (i = 0; i < n; i++) {
|
||||
stack.push(sc.nextInt());
|
||||
}
|
||||
sc.close();
|
||||
reverseStack(stack);
|
||||
System.out.println("The reversed stack is:");
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.print(stack.peek() + ",");
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,16 +49,15 @@ public class ReverseStack {
|
||||
|
||||
private static void insertAtBottom(Stack<Integer> stack, int element) {
|
||||
if (stack.isEmpty()) {
|
||||
// When stack is empty, insert the element so it will be present at the bottom of the
|
||||
// stack
|
||||
// When stack is empty, insert the element so it will be present at
|
||||
// the bottom of the stack
|
||||
stack.push(element);
|
||||
return;
|
||||
}
|
||||
|
||||
int ele = stack.peek();
|
||||
/*Keep popping elements till stack becomes empty. Push the elements once the topmost element
|
||||
has moved to the bottom of the stack.
|
||||
*/
|
||||
// Keep popping elements till stack becomes empty. Push the elements
|
||||
// once the topmost element has moved to the bottom of the stack.
|
||||
stack.pop();
|
||||
insertAtBottom(stack, element);
|
||||
|
||||
|
Reference in New Issue
Block a user