Java 深度优先遍历与调整矩阵

Java 深度优先遍历与调整矩阵,java,graph,Java,Graph,我正在尝试深度优先遍历。我不知道我是否已经接近了。现在它正在打印13 4 5。它应该是打印1 2 4 7 3 5 6。任何帮助或建议都将不胜感激。谢谢。:) 类别: public class myGraphs { Stack<Integer> st; int vFirst; int[][] adjMatrix; int[] isVisited = new int[7]; public myGraphs(int[][] Mat

我正在尝试深度优先遍历。我不知道我是否已经接近了。现在它正在打印13 4 5。它应该是打印1 2 4 7 3 5 6。任何帮助或建议都将不胜感激。谢谢。:)

类别:

 public class myGraphs {
     Stack<Integer> st;
     int vFirst;

     int[][] adjMatrix;
     int[] isVisited = new int[7];


     public myGraphs(int[][] Matrix) {
         this.adjMatrix = Matrix;
         st = new Stack<Integer>();
         int i;
         int[] node = {1, 2, 3, 4, 5, 6, 7};
         int firstNode = node[0];

         for (i = 1; i < node.length - 1; i++) {
             depthFirst(firstNode, node[i]);
         }
    }

    public void depthFirst(int vFirst, int n) {
        int v, i;

        st.push(vFirst);

        while (!st.isEmpty()) {
            v = st.pop();
            if (isVisited[v]==0) {
                System.out.print("\n"+v);
                isVisited[v]=1;
            }

            for ( i=1;i<=n;i++) {
                if ((adjMatrix[v][i] == 1) && (isVisited[i] == 0)) {
                    st.push(v);
                    isVisited[i]=1;
                    System.out.print(" " + i);
                    v = i;
                }
            }
        }
    }

    //
    public static void main(String[] args) {     
        // 1  2  3  4  5  6  7
        int[][] adjMatrix = { {0, 1, 1, 0, 0, 0, 0},
                              {1, 0, 0, 1, 1, 1, 0},
                              {1, 0, 0, 0, 0, 0, 1},
                              {0, 1, 0, 0, 0, 0, 1},
                              {0, 1, 0, 0, 0, 0, 1},
                              {0, 1, 0, 0, 0, 0 ,0},
                              {0, 0, 1, 1, 1, 0, 0}  };

       new myGraphs(adjMatrix);
     }
}
公共类myGraphs{
斯塔克街;
int vFirst;
int[][]调整矩阵;
int[]isvisted=新int[7];
公共myGraphs(int[][]矩阵){
这个矩阵=矩阵;
st=新堆栈();
int i;
int[]节点={1,2,3,4,5,6,7};
int firstNode=node[0];
对于(i=1;i对于(i=1;i如果您正在查看深度优先遍历,那么下面是您应该进行的代码更改

1) 首先将您的节点数组声明为
int[]node={0,1,2,3,4,5,6}
。这样做是为了避免数组索引开始(即0)和节点开始编号(即1)。因此,现在我们假设节点1的新名称为0,节点2为1……而节点7为6

2) 而不是做

for (i = 1; i < node.length-1; i++){
     depthFirst(firstNode, node[i]);
 } 
for(i=1;i
在MyGraph中,您需要: 深度优先(firstNode,7)


3) 如果有人在寻找(i=1;iC#中的工作/测试解决方案,则使用depthFirst代替

using System;
using System.Collections.Generic;

namespace GraphAdjMatrixDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // 0  1  2  3  4  5  6
            int[,] matrix = {     {0, 1, 1, 0, 0, 0, 0},
                                  {1, 0, 0, 1, 1, 1, 0},
                                  {1, 0, 0, 0, 0, 0, 1},
                                  {0, 1, 0, 0, 0, 0, 1},
                                  {0, 1, 0, 0, 0, 0, 1},
                                  {0, 1, 0, 0, 0, 0 ,0},
                                  {0, 0, 1, 1, 1, 0, 0}  };

            bool[] visitMatrix = new bool[matrix.GetLength(0)];
            Program ghDemo = new Program();

            for (int lpRCnt = 0; lpRCnt < matrix.GetLength(0); lpRCnt++)
            {
                for (int lpCCnt = 0; lpCCnt < matrix.GetLength(1); lpCCnt++)
                {
                    Console.Write(string.Format(" {0}  ", matrix[lpRCnt, lpCCnt]));
                }
                Console.WriteLine();
            }

            Console.Write("\nDFS Recursive : ");
            ghDemo.DftRecursive(matrix, visitMatrix, 0);
            Console.Write("\nDFS Iterative : ");
            ghDemo.DftIterative(matrix, 0);

            Console.Read();
        }

        //====================================================================================================================================

        public void DftRecursive(int[,] srcMatrix, bool[] visitMatrix, int vertex)
        {
            visitMatrix[vertex] = true;
            Console.Write(vertex + 1 + "  ");

            for (int neighbour = 0; neighbour < srcMatrix.GetLength(0); neighbour++)
            {
                if (visitMatrix[neighbour] == false && srcMatrix[vertex, neighbour] == 1)
                {
                    DftRecursive(srcMatrix, visitMatrix, neighbour);
                }
            }
        }

        public void DftIterative(int[,] srcMatrix, int srcVertex)
        {
            bool[] visited = new bool[srcMatrix.GetLength(0)];

            Stack<int> vertexStack = new Stack<int>();
            vertexStack.Push(srcVertex);

            while (vertexStack.Count > 0)
            {
                int vertex = vertexStack.Pop();

                if (visited[vertex] == true)
                    continue;

                Console.Write(vertex + 1 + "  ");
                visited[vertex] = true;

                for (int neighbour = 0; neighbour < srcMatrix.GetLength(0); neighbour++) 
                //for (int neighbour = srcMatrix.GetLength(0) - 1; neighbour >= 0; neighbour--)// To make same as recursive
                {
                    if (srcMatrix[vertex, neighbour] == 1 && visited[neighbour] == false)
                    {
                        vertexStack.Push(neighbour);
                    }
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
命名空间GraphAdjMatrixDemo
{
公共课程
{
公共静态void Main(字符串[]args)
{
// 0  1  2  3  4  5  6
int[,]矩阵={{0,1,1,0,0,0,0},
{1, 0, 0, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0 ,0},
{0, 0, 1, 1, 1, 0, 0}  };
bool[]visitMatrix=新bool[matrix.GetLength(0)];
程序ghDemo=新程序();
对于(int-lpRCnt=0;lpRCnt0)
{
int vertex=vertexStack.Pop();
如果(已访问[顶点]==真)
继续;
控制台。写入(顶点+1+“”);
访问[顶点]=真;
for(int nexter=0;nexter=0;nexter--)//使其与递归相同
{
if(srcMatrix[vertex,neighbor]==1&&visted[neighbor]==false)
{
vertexStack.Push(邻居);
}
}
}
}
}
}

为了使迭代的显示顺序与递归相同,我们需要按相反的顺序将相邻元素进行堆栈。从Amit answer中获取此逻辑,深度优先搜索什么?这是对图形的深度搜索。但你在搜索什么?你必须在这里定义“深度”。根据我的阅读,我期望[1 2 4 7 3 5 6],因为7可以转到3和5。OP的意思是说深度优先遍历。当我刚刚将int[]节点更改为0-6时,它实际上起了作用。除了firstnode,我甚至不需要int[]节点。谢谢你的帮助。Saurabh,我刚刚测试了你的代码,它正在打印1,2,4,7,5,6,3而不是1,2,4,7,3,5,6。你能测试并确认吗?
using System;
using System.Collections.Generic;

namespace GraphAdjMatrixDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // 0  1  2  3  4  5  6
            int[,] matrix = {     {0, 1, 1, 0, 0, 0, 0},
                                  {1, 0, 0, 1, 1, 1, 0},
                                  {1, 0, 0, 0, 0, 0, 1},
                                  {0, 1, 0, 0, 0, 0, 1},
                                  {0, 1, 0, 0, 0, 0, 1},
                                  {0, 1, 0, 0, 0, 0 ,0},
                                  {0, 0, 1, 1, 1, 0, 0}  };

            bool[] visitMatrix = new bool[matrix.GetLength(0)];
            Program ghDemo = new Program();

            for (int lpRCnt = 0; lpRCnt < matrix.GetLength(0); lpRCnt++)
            {
                for (int lpCCnt = 0; lpCCnt < matrix.GetLength(1); lpCCnt++)
                {
                    Console.Write(string.Format(" {0}  ", matrix[lpRCnt, lpCCnt]));
                }
                Console.WriteLine();
            }

            Console.Write("\nDFS Recursive : ");
            ghDemo.DftRecursive(matrix, visitMatrix, 0);
            Console.Write("\nDFS Iterative : ");
            ghDemo.DftIterative(matrix, 0);

            Console.Read();
        }

        //====================================================================================================================================

        public void DftRecursive(int[,] srcMatrix, bool[] visitMatrix, int vertex)
        {
            visitMatrix[vertex] = true;
            Console.Write(vertex + 1 + "  ");

            for (int neighbour = 0; neighbour < srcMatrix.GetLength(0); neighbour++)
            {
                if (visitMatrix[neighbour] == false && srcMatrix[vertex, neighbour] == 1)
                {
                    DftRecursive(srcMatrix, visitMatrix, neighbour);
                }
            }
        }

        public void DftIterative(int[,] srcMatrix, int srcVertex)
        {
            bool[] visited = new bool[srcMatrix.GetLength(0)];

            Stack<int> vertexStack = new Stack<int>();
            vertexStack.Push(srcVertex);

            while (vertexStack.Count > 0)
            {
                int vertex = vertexStack.Pop();

                if (visited[vertex] == true)
                    continue;

                Console.Write(vertex + 1 + "  ");
                visited[vertex] = true;

                for (int neighbour = 0; neighbour < srcMatrix.GetLength(0); neighbour++) 
                //for (int neighbour = srcMatrix.GetLength(0) - 1; neighbour >= 0; neighbour--)// To make same as recursive
                {
                    if (srcMatrix[vertex, neighbour] == 1 && visited[neighbour] == false)
                    {
                        vertexStack.Push(neighbour);
                    }
                }
            }
        }
    }
}