Java程序,用于显示使用DFS的无向图中的连接组件数量以及用户提供的邻接矩阵

时间:2014-12-24 19:24:34

标签: java components depth-first-search

正如标题所说,我需要创建一个Java程序,使用用户提供的邻接矩阵计算无向图中的连接组件数。我花了很多时间并设法成功获得邻接矩阵并计算有多少顶点没有连接到我的"源"顶点。但是,我无法将其包装起来并计算连接的组件。这对我来说太难了。请帮忙,谢谢。这是我的代码的当前状态:

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
import java.util.Arrays;

public class UndirectedConnectivityDfs
{
    private Stack<Integer> stack;

    public UndirectedConnectivityDfs() 
    {
        stack = new Stack<Integer>();
    } 

    public void dfs(int adjacency_matrix[][], int source)
    {
        int number_of_nodes = adjacency_matrix[source].length - 1;  
        int visited[] = new int[number_of_nodes + 1];   
        visited[0]=1;
        int element = source;       
        int i = source;
        int cc=1;
          for (int k = 1; k <= number_of_nodes; k++){
             if (visited[k] == 0) 
            {
         visited[source] = 1;       
        stack.push(source);        
        while (!stack.isEmpty())
        {
            element = stack.peek();
            i = element;    
        while (i <= number_of_nodes)
        {
                if (adjacency_matrix[element][i] == 1 && visited[i] == 0)
            {
                    stack.push(i);
                    visited[i] = 1;
                    element = i;
                    i = 1;
                continue;
                }
                i++;
        }
            stack.pop();    
        }
        boolean connected = false;

        for (int vertex = 1; vertex <= number_of_nodes; vertex++)
        {
            if (visited[vertex] == 1) 
            {
                connected = true;
            } else
            {
                connected = false;
                            }
        }

           }else
        {
            System.out.print("There are ");
            System.out.print(cc);
            System.out.println(" connected compoments");
        }
}
}

    public static void main(String...arg)
    {
        int number_of_nodes, source;
        Scanner scanner = null;
    try
        {
        System.out.println("Enter the number of nodes in the graph");
            scanner = new Scanner(System.in);
            number_of_nodes = scanner.nextInt();

        int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
        System.out.println("Enter the adjacency matrix");
        for (int i = 1; i <= number_of_nodes; i++)
           for (int j = 1; j <= number_of_nodes; j++)
                   adjacency_matrix[i][j] = scanner.nextInt();

        for (int i = 1; i <= number_of_nodes; i++)
            {
                for (int j = 1; j <= number_of_nodes; j++)
                {   
                     if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
                     {
                         adjacency_matrix[j][i] = 1;
                     }
                }
            }           

        System.out.println("Enter the source for the graph");
            source = scanner.nextInt(); 

            UndirectedConnectivityDfs undirectedConnectivity= new UndirectedConnectivityDfs();
            undirectedConnectivity.dfs(adjacency_matrix, source);   

        }catch(InputMismatchException inputMismatch)
        {
            System.out.println("Wrong Input format");
        }   
        scanner.close();    
    }   
}

1 个答案:

答案 0 :(得分:0)

你的解决方案差不多完成了。你必须调整变量&#39;范围在检查visited数组的for循环中。此外,您必须使用从0开始的索引,因为您可能会导致ArrayIndexOutOfBoundsException。此外,您不必将源作为输入。 这是固定代码:

public class UndirectedConnectivityDfs
{
    private Stack<Integer> stack;
    public UndirectedConnectivityDfs() 
    {
            stack = new Stack<Integer>();
    } 

    public void dfs(int adjacency_matrix[][])
    {
        int number_of_nodes = adjacency_matrix[0].length;   
        int visited[] = new int[number_of_nodes];       
        int cc = 0;
        for  (int vertex = 0; vertex < number_of_nodes; vertex++)
        {
            if (visited[vertex] == 0)
            {   
                int element = vertex;               
                int i = vertex;     
                visited[vertex] = 1;
                cc++;
                stack.push(vertex);
                while (!stack.isEmpty())
                {
                    element = stack.peek();
                    i = element;    
                    while (i < number_of_nodes)
                    {
                        if (adjacency_matrix[element][i] == 1 && visited[i] == 0)
                        {
                            stack.push(i);
                            visited[i] = 1;
                            element = i;
                            i = 1;
                            continue;
                            }
                            i++;
                    }
                    stack.pop();    
                }
            }
        }
        System.out.println("Number of Connected Components: " + cc);
    }

    public static void main(String...arg)
    {
        int number_of_nodes;
        Scanner scanner = null;
        try
        {
            System.out.println("Enter the number of nodes in the graph");
            scanner = new Scanner(System.in);

            number_of_nodes = scanner.nextInt();
            int adjacency_matrix[][] = new int[number_of_nodes][number_of_nodes];

            System.out.println("Enter the adjacency matrix");

            for (int i = 0; i < number_of_nodes; i++)
                for (int j = 0; j < number_of_nodes; j++)
                       adjacency_matrix[i][j] = scanner.nextInt();

            for (int i = 0; i < number_of_nodes; i++)
            {
                for (int j = 0; j < number_of_nodes; j++)
                {   
                    if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
                    {
                            adjacency_matrix[j][i] = 1;
                    }
                    }
            }           
            UndirectedConnectivityDfs undirectedConnectivity= new UndirectedConnectivityDfs();
            undirectedConnectivity.dfs(adjacency_matrix);   

        }catch(InputMismatchException inputMismatch)
        {
            System.out.println("Wrong Input format");
        }   
        scanner.close();    
    }   
}