-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
009e8f6
commit 41441b1
Showing
3 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
Name: Multisolver - Smallest, Longest, Ceil, Floor, Kthlargest Path | ||
Source: PepCoding | ||
Link: https://www.pepcoding.com/resources/online-java-foundation/graphs/mutilsovler-graph-official/ojquestion | ||
Statement: You are required to find and print the values of | ||
1 Smallest path and it's weight separated by an "@" | ||
2 Largest path and it's weight separated by an "@" | ||
3 Just Larger path (than criteria in terms of weight) and it's weight separated by an "@" | ||
4 Just smaller path (than criteria in terms of weight) and it's weight separated by an "@" | ||
5 Kth largest path and it's weight separated by an "@" | ||
*/ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class MultiSolver { | ||
static class Edge { | ||
int src; | ||
int nbr; | ||
int wt; | ||
|
||
Edge(int src, int nbr, int wt) { | ||
this.src = src; | ||
this.nbr = nbr; | ||
this.wt = wt; | ||
} | ||
} | ||
|
||
static class Pair implements Comparable < Pair > { | ||
int wsf; | ||
String psf; | ||
|
||
Pair(int wsf, String psf) { | ||
this.wsf = wsf; | ||
this.psf = psf; | ||
} | ||
|
||
public int compareTo(Pair o) { | ||
return this.wsf - o.wsf; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
int vtces = Integer.parseInt(br.readLine()); | ||
ArrayList < Edge > [] graph = new ArrayList[vtces]; | ||
for (int i = 0; i < vtces; i++) { | ||
graph[i] = new ArrayList < > (); | ||
} | ||
|
||
int edges = Integer.parseInt(br.readLine()); | ||
for (int i = 0; i < edges; i++) { | ||
String[] parts = br.readLine().split(" "); | ||
int v1 = Integer.parseInt(parts[0]); | ||
int v2 = Integer.parseInt(parts[1]); | ||
int wt = Integer.parseInt(parts[2]); | ||
graph[v1].add(new Edge(v1, v2, wt)); | ||
graph[v2].add(new Edge(v2, v1, wt)); | ||
} | ||
|
||
int src = Integer.parseInt(br.readLine()); | ||
int dest = Integer.parseInt(br.readLine()); | ||
|
||
int criteria = Integer.parseInt(br.readLine()); | ||
int k = Integer.parseInt(br.readLine()); | ||
|
||
boolean[] visited = new boolean[vtces]; | ||
multisolver(graph, src, dest, visited, criteria, k, src + "", 0); | ||
|
||
System.out.println("Smallest Path = " + spath + "@" + spathwt); | ||
System.out.println("Largest Path = " + lpath + "@" + lpathwt); | ||
System.out.println("Just Larger Path than " + criteria + " = " + cpath + "@" + cpathwt); | ||
System.out.println("Just Smaller Path than " + criteria + " = " + fpath + "@" + fpathwt); | ||
System.out.println(k + "th largest path = " + pq.peek().psf + "@" + pq.peek().wsf); | ||
} | ||
|
||
|
||
|
||
static String spath; | ||
static Integer spathwt = Integer.MAX_VALUE; | ||
static String lpath; | ||
static Integer lpathwt = Integer.MIN_VALUE; | ||
static String cpath; | ||
static Integer cpathwt = Integer.MAX_VALUE; | ||
static String fpath; | ||
static Integer fpathwt = Integer.MIN_VALUE; | ||
static PriorityQueue < Pair > pq = new PriorityQueue < > (); | ||
public static void multisolver(ArrayList < Edge > [] graph, int src, int dest, boolean[] visited, int criteria, int k, String psf, int wsf) { | ||
|
||
if(src == dest) | ||
{ | ||
if(spathwt > wsf) | ||
{ | ||
spathwt = wsf; | ||
spath = psf; | ||
} | ||
|
||
if(lpathwt < wsf) | ||
{ | ||
lpathwt = wsf; | ||
lpath = psf; | ||
} | ||
|
||
if(wsf > criteria && wsf < cpathwt) | ||
{ | ||
cpathwt = wsf; | ||
cpath = psf; | ||
} | ||
|
||
if(wsf < criteria && wsf > fpathwt) | ||
{ | ||
fpathwt = wsf; | ||
fpath = psf; | ||
} | ||
|
||
if(pq.size() < k) | ||
{ | ||
pq.add(new Pair(wsf, psf)); | ||
} | ||
else | ||
{ | ||
if(pq.peek().wsf < wsf) | ||
{ | ||
pq.remove(); | ||
pq.add(new Pair(wsf, psf)); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
visited[src] = true; | ||
|
||
for( Edge edge : graph[src]) | ||
{ | ||
if(!visited[edge.nbr]) | ||
{ | ||
multisolver(graph, edge.nbr, dest, visited, criteria, k, psf +edge.nbr , wsf + edge.wt); | ||
} | ||
} | ||
|
||
visited[src] = false; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Name: Perfect Friends | ||
Source: PepCoding | ||
Link: https://www.pepcoding.com/resources/online-java-foundation/graphs/perfect-friends-official/ojquestion | ||
Statement: You are given a number n (representing the number of students). Each student will have an id | ||
from 0 to n - 1. You are given a number k (representing the number of clubs). In the next k lines, two numbers are given separated by a space. The numbers are ids of | ||
students belonging to same club. You have to find in how many ways can we select a pair of students such that both students are from different clubs. | ||
*/ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class PerfectFriends { | ||
|
||
public static class Edge { | ||
int src; | ||
int nbr; | ||
|
||
Edge(int src, int nbr) | ||
{ | ||
this.src = src; | ||
this.nbr = nbr; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
int n = Integer.parseInt(br.readLine()); | ||
int k = Integer.parseInt(br.readLine()); | ||
ArrayList < Edge > [] graph = new ArrayList[n]; | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
graph[i] = new ArrayList<>(); | ||
} | ||
|
||
for(int i=0; i<k; i++) | ||
{ | ||
String [] parts = br.readLine().split(" "); | ||
int a = Integer.parseInt(parts[0]); | ||
int b = Integer.parseInt(parts[1]); | ||
graph[a].add(new Edge(a, b)); | ||
graph[b].add(new Edge(b, a)); | ||
} | ||
|
||
ArrayList < ArrayList < Integer >> comps = new ArrayList < > (); | ||
|
||
|
||
|
||
boolean [] visited = new boolean[n]; | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
if(!visited[i]) | ||
{ | ||
ArrayList<Integer> comp = new ArrayList<>(); | ||
perfectFriends(graph, i, comp, visited); | ||
comps.add(comp); | ||
} | ||
} | ||
|
||
int count =0; | ||
for(int i=0; i<comps.size(); i++) | ||
{ | ||
for(int j=i+1; j<comps.size(); j++) | ||
{ | ||
count+= (comps.get(i).size() * comps.get(j).size()); | ||
} | ||
} | ||
|
||
System.out.println(count); | ||
|
||
} | ||
|
||
public static void perfectFriends(ArrayList < Edge > [] graph , int src, ArrayList<Integer> comp,boolean [] visited){ | ||
|
||
visited[src] = true; | ||
comp.add(src); | ||
|
||
for(Edge edge: graph[src]) | ||
{ | ||
if(!visited[edge.nbr]) | ||
{ | ||
perfectFriends(graph, edge.nbr, comp, visited); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Name: Minimum Wire Required To Connect All Pcs (Prim')s Algo | ||
Source: PepCoding | ||
Link: https://www.pepcoding.com/resources/online-java-foundation/graphs/minimum-wire-to-connect-all-pcs-official/ojquestion | ||
Statement: You are given a graph and a source vertex. The vertices represent computers and the edges | ||
represent length of LAN wire required to connect them. You are required to find the minimum length of wire required to connect all PCs over a network. | ||
Print the output in terms of which all PCs need to be connected, and the length of wire between them. | ||
*/ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class PrimsAlgo { | ||
static class Edge { | ||
int src; | ||
int nbr; | ||
int wt; | ||
|
||
Edge(int src, int nbr, int wt) { | ||
this.src = src; | ||
this.nbr = nbr; | ||
this.wt = wt; | ||
} | ||
} | ||
|
||
public static class Pair implements Comparable<Pair> | ||
{ | ||
int vtx; | ||
int src; | ||
int wt; | ||
|
||
Pair(int vtx, int src, int wt) | ||
{ | ||
this.vtx = vtx; | ||
this.src = src; | ||
this.wt = wt; | ||
} | ||
|
||
public int compareTo(Pair oth) | ||
{ | ||
return this.wt - oth.wt; | ||
} | ||
|
||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
int vtces = Integer.parseInt(br.readLine()); | ||
ArrayList < Edge > [] graph = new ArrayList[vtces]; | ||
for (int i = 0; i < vtces; i++) { | ||
graph[i] = new ArrayList < > (); | ||
} | ||
|
||
int edges = Integer.parseInt(br.readLine()); | ||
for (int i = 0; i < edges; i++) { | ||
String[] parts = br.readLine().split(" "); | ||
int v1 = Integer.parseInt(parts[0]); | ||
int v2 = Integer.parseInt(parts[1]); | ||
int wt = Integer.parseInt(parts[2]); | ||
graph[v1].add(new Edge(v1, v2, wt)); | ||
graph[v2].add(new Edge(v2, v1, wt)); | ||
} | ||
|
||
boolean [] visited = new boolean[vtces]; | ||
PriorityQueue<Pair> que = new PriorityQueue<>(); | ||
que.add(new Pair(0, -1, 0)); | ||
|
||
while(que.size() > 0) | ||
{ | ||
Pair top = que.remove(); | ||
|
||
if(visited[top.vtx]) | ||
{ | ||
continue; | ||
} | ||
|
||
visited[top.vtx] = true; | ||
|
||
if(top.src > -1) | ||
{ | ||
System.out.println("[" + top.vtx + "-" + top.src + "@" + top.wt + "]"); | ||
} | ||
|
||
for(Edge edge: graph[top.vtx]) | ||
{ | ||
if(!visited[edge.nbr]) | ||
{ | ||
que.add(new Pair(edge.nbr, edge.src, edge.wt)); | ||
} | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
} |