Java program to print bfs traversal from a given source vertex



Download 25,5 Kb.
Sana07.06.2021
Hajmi25,5 Kb.
#65969
Bog'liq
Java program to-WPS Office


// Java program to print BFS traversal from a given source vertex.

// BFS(int s) traverses vertices reachable from s.

import java.io.*;

import java.util.*;

// This class represents a directed graph using adjacency list

// representation

class Graph

{

private int V; // No. of vertices



private LinkedList adj[]; //Adjacency Lists

// Constructor

Graph(int v)

{

V = v;



adj = new LinkedList[v];

for (int i=0; i

adj[i] = new LinkedList();

}

// Function to add an edge into the graph



void addEdge(int v,int w)

{

adj[v].add(w);



}

// prints BFS traversal from a given source s

void BFS(int s)

{

// Mark all the vertices as not visited(By default



// set as false)

boolean visited[] = new boolean[V];

// Create a queue for BFS

LinkedList queue = new LinkedList();

// Mark the current node as visited and enqueue it

visited[s]=true;

queue.add(s);

while (queue.size() != 0)

{

// Dequeue a vertex from queue and print it



s = queue.poll();

System.out.print(s+" ");

// Get all adjacent vertices of the dequeued vertex s

// If a adjacent has not been visited, then mark it

// visited and enqueue it

Iterator i = adj[s].listIterator();

while (i.hasNext())

{

int n = i.next();



if (!visited[n])

{

visited[n] = true;



queue.add(n);

}

}



}

}

// Driver method to



public static void main(String args[])

{

Graph g = new Graph(4);



g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(1, 2);

g.addEdge(2, 0);

g.addEdge(2, 3);

g.addEdge(3, 3);

System.out.println("Following is Breadth First Traversal "+

"(starting from vertex 2)");

g.BFS(2);

}

}



// BFS algorithm in Java

import java.util.*;

public class Graph {

private int V;

private LinkedList adj[];

// Create a graph

Graph(int v) {

V = v;


adj = new LinkedList[v];

for (int i = 0; i < v; ++i)

adj[i] = new LinkedList();

}

// Add edges to the graph



void addEdge(int v, int w) {

adj[v].add(w);

}

// BFS algorithm



void BFS(int s) {

boolean visited[] = new boolean[V];

LinkedList queue = new LinkedList();

visited[s] = true;

queue.add(s);

while (queue.size() != 0) {

s = queue.poll();

System.out.print(s + " ");

Iterator i = adj[s].listIterator();

while (i.hasNext()) {

int n = i.next();

if (!visited[n]) {

visited[n] = true;

queue.add(n);

}

}

}



}

public static void main(String args[]) {

Graph g = new Graph(4);

g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(1, 2);

g.addEdge(2, 0);

g.addEdge(2, 3);

g.addEdge(3, 3);

System.out.println("Following is Breadth First Traversal " + "(starting from vertex 2)");



g.BFS(2);

}

}

Download 25,5 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish