Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Graph_BFS(breadth-first-search algorithm)#7158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Divyansh1802 wants to merge4 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromDivyansh1802:GRAPH
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file addedGraph$Node.class
View file
Open in desktop
Binary file not shown.
Binary file addedGraph.class
View file
Open in desktop
Binary file not shown.
Binary file addedMain.class
View file
Open in desktop
Binary file not shown.
73 changes: 73 additions & 0 deletionsMain.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
import java.util.*;
class Graph{

static HashMap<String,List<Node>> adj; //creating a adjacency List {source ->destinations}

public Graph(){
this.adj=new HashMap<>(); //constructor initializes the adjacency list
}

static class Node{ //Node class to store the destination place and the distance frome source to destination
String dest;
int dist;

Node(String dest,int dist){
this.dest=dest;
this.dist=dist;
}
}

public static void insert(String src,String dest,int dist){
insert(src,new Node(dest,dist)); //Undirected weighted graph
insert(dest,new Node(src,dist));
}

private static void insert(String src,Node n){
if(adj.isEmpty() || !adj.containsKey(src)){
adj.put(src,new ArrayList<>());
}
adj.get(src).add(n);
}

public static void BFS(String src){ // BREADTH-FIRST-SEARCH
if(adj.isEmpty()) return;

HashSet<String> set=new HashSet<>();
Queue<String> q=new LinkedList<>(); //BFS uses Queue data structure
q.offer(src);
set.add(src);

while(!q.isEmpty()){
String node=q.poll();
System.out.print(node+" ");
List<Node> neighbours=adj.get(src);
if(neighbours!=null){
for(Node neighbour:neighbours){
if(neighbour!=null && !set.contains(neighbour.dest)){
q.offer(neighbour.dest);
set.add(neighbour.dest);
}
}
}
}
}

}

class Main{
public static void main(String[] args){
Graph graph=new Graph(); //instantiating Graph by making an object graph of GRAPH
graph.insert("DELHI","MUMBAI",990);
graph.insert("MUMBAI","PUNE",100);
graph.insert("PUNE","GOA",140);
graph.insert("BANGALORE","PUNE",453);
graph.insert("DELHI","PUNE",1056);
graph.insert("DELHI","HYDERABAD",1099);
graph.insert("VISHAKHAPATNAM","HYDERABAD",230);
graph.insert("BANGALORE","DELHI",1000);
graph.insert("CHENNAI","BANGALORE",600);
graph.insert("CHENNAI","DELHI",1300);

graph.BFS("DELHI");
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp