-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMin_Edges_From_Source_Destination.java
More file actions
90 lines (80 loc) · 1.91 KB
/
Min_Edges_From_Source_Destination.java
File metadata and controls
90 lines (80 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*Algorithm
stpe1:-count all path from source to destination using Bfs or Dfs here i am using Dfs
step2:and select mimimum of them
*/
//---------------------------------------
import java.util.Scanner;
import java.util.ArrayList;
class Min_Edge
{
int v;
int min=Integer.MAX_VALUE;
//ArrayList is use for representation of graph
ArrayList<Integer> adj[];
Min_Edge(int v)
{
this.v=v;
adj=new ArrayList[v];
for(int i=0;i<v;i++)
{
adj[i]=new ArrayList<Integer>();
}
}
//Add Edge Between source and destination Vertices
void addEdge(int src,int dest)
{
adj[src].add(dest);
adj[dest].add(src);
}
//Main Method for count minmumu Edge between source and destination
void minEdge(int src,int dest)
{
boolean []visited=new boolean[v];
ArrayList<Integer> edge=new ArrayList<Integer>();
edge.add(src);
allEdge(src,dest,visited,edge);
System.out.println(min);
}
//Utility methode which call by addEdge Method for implementaion of Dfs
void allEdge(int src,int dest,boolean visited[],ArrayList<Integer> edge)
{
visited[src]=true;
if(src==dest)
{
int count=0;
for(Integer j:edge)
{
count++;
}
if(count-1<min)
{
min=count-1;
}
}
for(Integer i:adj[src])
{
if(!visited[i])
{
edge.add(i);
allEdge(i,dest,visited,edge);
edge.remove(i);
}
}
visited[src]=false;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int v=sc.nextInt();
int e=sc.nextInt();
Min_Edge g=new Min_Edge(v);
for(int i=1;i<=e;i++)
{
int src=sc.nextInt();
int dest=sc.nextInt();
g.addEdge(src,dest);
}
int src=sc.nextInt();
int dest=sc.nextInt();
g.minEdge(src,dest);
}
}