简体   繁体   中英

Why BFS stuck in Graph Node representation?

I have created graph using Node and Edge class.

When I call traverseBFS method from start = 0 . then It just stuck. cannot proceed further. When I use similar approach with HashMap<Integer,ArrayList<Integer>> this algorithm runs properly. Please help me how to fix this.

Complete Code

import java.util.*;
import java.io.*;

public class Dijkstra {
    static class Node {
        public int id;
        public long dist;
        public int par;

        public Node(int a, long d, int b) {
            id = a;
            dist = d;
            par = b;
        }
    }

    static class Edge {
        int to;
        int weight;

        public Edge(int a, int b) {
            to = a;
            weight = b;
        }
    }

    static int vert;
    static ArrayList<LinkedList<Edge>> list;
    static int[] parent;
    static long[] distance;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        vert = sc.nextInt();
        int edges = sc.nextInt();
        list = new ArrayList<>();
        parent = new int[vert + 1];
        distance = new long[vert + 1];

        for (int i = 0; i <= vert; i++) {
            list.add(i, new LinkedList<Edge>());
        }

        for (int i = 0; i < edges; i++) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            int w = sc.nextInt();
            list.get(u).add(new Edge(v, w));
            list.get(v).add(new Edge(u, w));
        }
        traverseBFS(0);
    }
    public static void traverseBFS(int start) {
        System.out.print("\nBFS >> \n");
        boolean visited[] = new boolean[vert];
        LinkedList<Integer> q = new LinkedList<>();
        q.add(start);
        visited[start] = true;

        while (!q.isEmpty()) {
            int s = q.poll();
            System.out.print(s + " ");

            LinkedList<Edge> temp = list.get(s);
            for (Edge var : temp) {
                if (!visited[var.to]) {
                    visited[var.to] = true;
                    q.add(var.to);

                }
            }
        }
    }

}


Input

5 6

1 2 2

2 5 5

2 3 4

1 4 1

4 3 3

3 5 1

Output

BFS >>

0

When posting mre consider hard coding test data, to make it easier to run a test:

public static void main(String[] args) {

    int[][] neighbours = {

            {1,2,2},

            {2,5,5},

            {2,3,4},

            {1,4,1},

            {4,3,3},

            {3,5,1}
    };

    vert = 5;

    list = new ArrayList<>();
    parent = new int[vert + 1];
    distance = new long[vert + 1];

    for (int i = 0; i <= vert; i++) {
        list.add(i, new LinkedList<Edge>());
    }

    for (int i = 0; i < neighbours.length; i++) {
        int u = neighbours[i][0];
        int v = neighbours[i][1];
        int w = neighbours[i][2];
        list.get(u).add(new Edge(v, w));
        list.get(v).add(new Edge(u, w));
    }

    traverseBFS(0);
}

A simple print out the graph created shows that node 0 is not connected to any other node:

Node 0 connected: []
Node 1 connected: [to 2, to 4]
Node 2 connected: [to 1, to 5, to 3]
Node 3 connected: [to 2, to 4, to 5]
Node 4 connected: [to 1, to 3]
Node 5 connected: [to 2, to 3]


To simplify the printout add toString method to Edge:

static class Edge {
    int to;
    int weight;

    public Edge(int a, int b) {
        to = a;
        weight = b;
    }

    @Override
    public String toString() {
        return "to "+to;
    }
}

and use

  for(int node = 0; node < list.size(); node ++){
     System.out.println("Node "+node +" connected: " + list.get(node));
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM