使用邻接矩阵(Python或伪代码)拓扑排序的无权有向无环图中的最短和最长路径

使用邻接矩阵(Python或伪代码)拓扑排序的无权有向无环图中的最短和最长路径,python,graph-theory,directed-acyclic-graphs,Python,Graph Theory,Directed Acyclic Graphs,我正试图解决一个交给我做作业的问题,我真的觉得我对算法思考过度了,希望这里的人能把我推向正确的方向 我将得到一个输入txt文件,如下所示: 1 // n number of graphs 4 // n number of vertices for graph 1 4 // n number of edges for graph 1 1 2 // edges given in pairs 2 3 2 4 3 4 我应该用这些数据来收集n个表示图的邻接矩阵。然后,我需要对邻接

我正试图解决一个交给我做作业的问题,我真的觉得我对算法思考过度了,希望这里的人能把我推向正确的方向

我将得到一个输入txt文件,如下所示:

1    // n number of graphs
4    // n number of vertices for graph 1
4    // n number of edges for graph 1
1 2  // edges given in pairs
2 3
2 4
3 4 
我应该用这些数据来收集n个表示图的邻接矩阵。然后,我需要对邻接矩阵中的数据实施3种方法:

  • findLongestPath(),它将返回图形中最长的路径
  • findShortestPath(),它将返回图形中的最短路径
  • totalNumberPaths(),它将返回图形中不同数量的路径
  • 我在执行前两部分时遇到了困难。这就是我到目前为止所做的:

    def main():
    
    
    numGraphs = input()
    
    for x in xrange(0, numGraphs):
        numVerts = input()
        numEdges = input()
        adjMat = [[0 for x in xrange(numVerts)] for x in xrange(numVerts)] 
        for x in xrange(0, numEdges):
            edges = raw_input()
            i, padding, j = edges.rpartition(" ")
    
            i = int(i)
            j = int(j)
    
            i -= 1
            j -= 1
    
            adjMat[i][j] = 1
    
    
        numPaths = [0 for x in xrange(numVerts)]
        numPaths[0] = 1 
    
        longest_path = 1
        shortest_path = numVerts
    
        for i in xrange(0, numVerts):
            current_path = 0
            for j in xrange(0, numVerts):
                if adjMat[i][j] == 1:
                    numPaths[j] += numPaths[i]
                    current_path += 1
    
            if current_path > longest_path:
                longest_path = current_path
            if current_path < shortest_path:
                shortest_path = current_path
    
        print "shortest: %d, longest: %d, total %d" % (shortest_path, longest_path, numPaths[numVerts-1])
    
     if __name__ == "__main__":
         main()
    

    我明白了。这是我的最终解决方案

    numGraphs = input()
    
    for x in xrange(0, numGraphs):
        numVerts = input()
        numEdges = input()
        adjMat = [[0 for x in xrange(numVerts)] for x in xrange(numVerts)] 
        for x in xrange(0, numEdges):
            edges = raw_input()
            i, padding, j = edges.rpartition(" ")
    
            i = int(i)
            j = int(j)
    
            i -= 1
            j -= 1
    
            adjMat[i][j] = 1
    
    
        numPaths = [0 for x in xrange(numVerts)]
        numPaths[0] = 1 
    
        currentPath = [0 for x in xrange(numVerts)]
        maxPath = 1
        minPath = numVerts -1
    
        for i in xrange(0, numVerts):
            for j in xrange(1, numVerts):
                if adjMat[i][j] == 1:
                    numPaths[j] += numPaths[i]
                    currentPath[j-i] += 1
                if (currentPath[j-i] is not 0):
                    minPath = currentPath[j-i]
                maxPath = max(currentPath)
    
        print "shortest: %d, longest: %d, total %d" % (minPath, maxPath, numPaths[numVerts-1])
    

    请将更新后的代码作为答案发布并接受。如果你确信它有效并且是你问题的正确解决方案,你应该回答你自己的问题(
    numGraphs = input()
    
    for x in xrange(0, numGraphs):
        numVerts = input()
        numEdges = input()
        adjMat = [[0 for x in xrange(numVerts)] for x in xrange(numVerts)] 
        for x in xrange(0, numEdges):
            edges = raw_input()
            i, padding, j = edges.rpartition(" ")
    
            i = int(i)
            j = int(j)
    
            i -= 1
            j -= 1
    
            adjMat[i][j] = 1
    
    
        numPaths = [0 for x in xrange(numVerts)]
        numPaths[0] = 1 
    
        currentPath = [0 for x in xrange(numVerts)]
        maxPath = 1
        minPath = numVerts -1
    
        for i in xrange(0, numVerts):
            for j in xrange(1, numVerts):
                if adjMat[i][j] == 1:
                    numPaths[j] += numPaths[i]
                    currentPath[j-i] += 1
                if (currentPath[j-i] is not 0):
                    minPath = currentPath[j-i]
                maxPath = max(currentPath)
    
        print "shortest: %d, longest: %d, total %d" % (minPath, maxPath, numPaths[numVerts-1])