0% found this document useful (0 votes)
3 views

Simpler Codes

The document contains multiple Java implementations of algorithms including Winner Tree, Heap Sort, BFS, DFS, Topological Sort, and Bellman-Ford. Each algorithm is encapsulated in a class with a main method that reads input, processes it, and outputs the results. The algorithms demonstrate various data structures and techniques for sorting and traversing graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Simpler Codes

The document contains multiple Java implementations of algorithms including Winner Tree, Heap Sort, BFS, DFS, Topological Sort, and Bellman-Ford. Each algorithm is encapsulated in a class with a main method that reads input, processes it, and outputs the results. The algorithms demonstrate various data structures and techniques for sorting and traversing graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

WINNER TREE

import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String[] strArr = input.split(" ");
int[] arr = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) {
arr[i] = Integer.parseInt(strArr[i]);
}
Arrays.sort(arr);
System.out.println(arr[1]);
}
}

HEAP SORT:
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N = sc.nextInt();
int[] arr = new int[N];
for(int i=0;i<N;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
for(int i=0;i<N;i++){
System.out.print(arr[i]+" ");
}
}
}

BFS: breadth first search


import java.util.*;
public class Main {
public static void bfs(int[][] graph, int n) {
boolean[] visited = new boolean[n];
int[] queue = new int[n];
int front = 0, rear = 0;
queue[rear++] = 0; // Start BFS from node 0
visited[0] = true;
System.out.print("BFS : ");
while (front < rear) {
int node = queue[front++];
System.out.print(node + " ");
for (int i = 0; i < n; i++) {
if (graph[node][i] == 1 && !visited[i]) {
queue[rear++] = i;
visited[i] = true;
}
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n == 0) {
System.out.println("Graph doesn't exist");
return;
}
int[][] graph = new int[n][n];
while (true) {
int u = sc.nextInt();
int v = sc.nextInt();
if (u == -1 && v == -1) break;
graph[u][v] = 1;
graph[v][u] = 1;
}
sc.close();
bfs(graph, n);
}
}

DFS:
import java.util.*;
public class Main {
public static void dfs(int[][] graph, int n) {
boolean[] visited = new boolean[n];
Stack<Integer> stack = new Stack<>();
stack.push(0);
System.out.print("0 ");
visited[0] = true;

while (!stack.isEmpty()) {
int node = stack.peek();
boolean hasUnvisitedNeighbor = false;
for (int i = 0; i < n; i++) {
if (graph[node][i] == 1 && !visited[i]) {
stack.push(i);
System.out.print(i + " ");
visited[i] = true;
hasUnvisitedNeighbor = true;
break;
}
}
if (!hasUnvisitedNeighbor) stack.pop();
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
if (n == 0 || m == 0) {
System.out.println("0");
return;
}
int[][] graph = new int[n][n];
for (int i = 0; i < m; i++) {
int u = sc.nextInt(), v = sc.nextInt();
graph[u][v] = 1;
graph[v][u] = 1;
}
sc.close();
dfs(graph, n);
}
}

Topological Sort
import java.util.*;
public class Main {
static void topologicalSort(int[][] graph, int n) {
boolean[] visited = new boolean[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
if (!visited[i])
dfs(graph, i, visited, stack);
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
System.out.println();
}
static void dfs(int[][] graph, int node, boolean[] visited, Stack<Integer> stack)
{
visited[node] = true;
for (int i = 0; i < graph.length; i++) {
if (graph[node][i] == 1 && !visited[i]) {
dfs(graph, i, visited, stack);
}
}
stack.push(node);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
if (n == 0 || m == 0) {
System.out.println("Graph doesn't exist");
return;
}

int[][] graph = new int[n][n];


for (int i = 0; i < m; i++) {
int u = sc.nextInt(), v = sc.nextInt();
graph[u][v] = 1;
}
sc.close();
topologicalSort(graph, n);
}
}

Bellman-Ford
import java.util.*;
public class Main {
static void bellmanFord(int[][] edges, int n, int m) {
int[] dist = new int[n];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[0] = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < m; j++) {
int u = edges[j][0], v = edges[j][1], w = edges[j][2];
if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
}
}
}
for (int j = 0; j < m; j++) {
int u = edges[j][0], v = edges[j][1], w = edges[j][2];
if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]) {
System.out.println("-1");
return;
}
}
for (int i = 0; i < n; i++) {
System.out.print((dist[i] == Integer.MAX_VALUE ? -1 : dist[i]) + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
if (n == 0 || m == 0) {
System.out.println("-1");
return;
}
int[][] edges = new int[m][3];
for (int i = 0; i < m; i++) {
edges[i][0] = sc.nextInt();
edges[i][1] = sc.nextInt();
edges[i][2] = sc.nextInt();
}
sc.close();
bellmanFord(edges, n, m);
}
}

You might also like