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

GraphCode

The document contains Java code for implementing a graph using an adjacency list representation, including methods to add edges and check for paths between vertices. It also includes a solution to Leetcode problem 1971, which determines if a valid path exists between two nodes in a graph. The input section provides a sample graph with 7 vertices and 8 edges, along with their respective weights.

Uploaded by

aditis.dis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

GraphCode

The document contains Java code for implementing a graph using an adjacency list representation, including methods to add edges and check for paths between vertices. It also includes a solution to Leetcode problem 1971, which determines if a valid path exists between two nodes in a graph. The input section provides a sample graph with 7 vertices and 8 edges, along with their respective weights.

Uploaded by

aditis.dis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;
public class D2_graphBasics {

public 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 void main(String[] args) {
Scanner sc = new Scanner(System.in);
int vtces = sc.nextInt();
int e = sc.nextInt();

ArrayList<Edge>[] graph = new ArrayList[vtces];


for(int i = 0; i < vtces; i++){
graph[i] = new ArrayList<>();
}

for(int i = 0; i < e; i++){

int u = sc.nextInt();
int v = sc.nextInt();
int wt = sc.nextInt();

graph[u].add(new Edge(u, v, wt));


graph[v].add(new Edge(v, u, wt));
}
boolean[] visited = new boolean[vtces];
System.out.println(0,6,graph,visited);
}
public static boolean hasPath(int src, int dest, ArrayList<Edge>[] graph,
boolean[] visited){
if(src == dest) return true;

visited[src] = true;
ArrayList<Edge> l = graph[src];
for(Edge e : l){
if(visited[e.nbr]==false){
boolean a = hasPath(e.nbr,dest,graph,visited);
if(a) return true;
}

return false;
}
}
}
-----------------------------------------------------------------------------------
-------
Leetcode 1971
Find if Path Exist in graph
Solution :
class Solution {
public boolean validPath(int n, int[][] edges, int src, int dest) {
List<Integer>[] graph = new ArrayList[n];
for(int i=0; i<n; i++){
graph[i] = new ArrayList<>();
}

for(int i=0; i<edges.length; i++){


int u = edges[i][0];
int v = edges[i][1];

graph[u].add(v);
graph[v].add(u);
}

boolean[] visited = new boolean[n];


return hasPath(graph,src,dest,visited);..
}

public boolean hasPath(List<Integer>[] graph, int src, int dest, boolean[] visited)
{
if(src==dest){
return true;
}

visited[src] = true;
List<Integer> l = graph[src];
for(Integer e : l){
if(visited[e]==false){
boolean a = hasPath(graph,e,dest,visited);
if(a) return true;
}
}
return false;
}
-----------------------------------------------------------------------------------
---
input :
7
8
0 1 10
1 2 10
2 3 10
0 3 40
3 4 5
4 5 3
5 6 3
4 6 8

You might also like