0% found this document useful (0 votes)
72 views3 pages

Level 4

This document contains code for finding the shortest distance between two nodes in a graph using breadth-first search (BFS). It includes classes and methods for representing an undirected graph using an adjacency list, adding edges to the graph, and performing a BFS to calculate distances and reconstruct the shortest path. The main method takes input for the number of vertices and edges, builds the graph, takes input for a starting and destination node, and calls the BFS method to find and return the shortest distance between those nodes.

Uploaded by

Rahik Ihttipas
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)
72 views3 pages

Level 4

This document contains code for finding the shortest distance between two nodes in a graph using breadth-first search (BFS). It includes classes and methods for representing an undirected graph using an adjacency list, adding edges to the graph, and performing a BFS to calculate distances and reconstruct the shortest path. The main method takes input for the number of vertices and edges, builds the graph, takes input for a starting and destination node, and calls the BFS method to find and return the shortest distance between those nodes.

Uploaded by

Rahik Ihttipas
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/ 3

import java.io.

*;
import java.util.*;
import java.util.LinkedList;

// This class represents an undirected graph using adjacency list


// representation
class Graph
{
private int V; // No. of vertices

private LinkedList<Integer> adj[] = new LinkedList[v];


int time = 0;
static final int NIL = -1;

{
V = v;
int i = 0;
while (i < v) {
adj[i] = new LinkedList();
++i;
}
}

void addEdge(int v, int w)


{
adj[v].add(w); // Add w to v's list.
adj[w].add(v); //Add v to w's list
}

// A recursive function using DFS


// u --> The vertex visited next
// visited[] --> keeps tract of vertices
// disc[] --> Stores discovery times of vertices
// parent[] --> Stores parent vertices
// ap[] --> Store articulation points
void APUtil(int u, boolean visited[], int disc[],
int low[], int parent[], boolean ap[])
{
public static int Closestdistance(ArrayList<ArrayList<Integer>>adj, int s,
int dest, int v){
int predicate[]= new int[v];
int distance[]= new int[v];
if (BFS(adj,s,destination,v,predicate,distance)==false){
System.out.println("Starting point and array are not connected");
return -1;
}
LinkedList<Integer>path=new LinkedList<Integer>();
int crawl=destination;
path.add(crawl);
while(predicate[crawl]!=-1){
path.add(predicate[crawl]);
crawl=predicate[crawl];
}

//System.out.println(" Moves "+distance[destination]); //


return distance[destination];
/*System.out.println("Path : ");
for(int i=path.size()-1; i>=0;i--){
System.out.println(path.get(i)+" ");
}*/
public static boolean BFS(ArrayList<ArrayList<Integer>>adj, int src, int
destination, int v, int predicate[], int distance[]){
LinkedList<Integer>queue= new LinkedList<Integer>();
boolean visited[]=new boolean[v];
for(int i=0;i<v;i++){
visited[i]=false;
distance[i]=Integer.MaxValue;
predicate[i]=-1;
}
visited[src]=true;
distance[src]=0;
queue.add(src);
while(!queue.isEmpty()){
int u=queue.remove();
for(int i=0;i<adj.get(u).size();i++){
if(visited[adj.get(u).get(i)]==false){

visited[adj.get(u).get(i)]=true;
distance[adj.get(u).get(i)]=dist[u]+1;
predicate[adj.get(u).get(i)]=u;
queue.add(adj.get(u).get(i));

if(adj.get(u).get(i)==dest)
return true;
}
}
}return false;
}

public static void main(String args[])


{
Scanner keyword= new Scanner(System.in);

System.out.println("Enter number of vertices, v: ");


int v=keyword.nextInt();

System.out.println("Enter number of connections,edge: ");


int e=keyword.nextInt();

ArrayList<ArrayList<Integer>>adj= new ArrayList<ArrayList<Integer>>(v);

for(int i=0;i<e;i++){
adj.add(new ArrayList<Integer>());

addEdge(adj,0,7);
addEdge(adj,0,8);
addEdge(adj,0,6);
addEdge(adj,1,2);
addEdge(adj,1,5);
addEdge(adj,2,0);
addEdge(adj,2,5);
addEdge(adj,3,4);
addEdge(adj,4,2);
addEdge(adj,3,1);
System.out.println("Enter Noras position: ");
int norasposition=keyword.nextInt();
}

You might also like