简体   繁体   English

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

[英]Shortest and longest path in a topologically sorted unweighted directed acyclic graph using an adjacency matrix (Python, or pseudo-code)

I'm trying to solve a problem I was given for homework and really feel like I'm overthinking the algorithm and hoping someone here can push me in the right direction. 我正在尝试解决我在做作业时遇到的一个问题,感觉就像是我对算法的思考过多,希望这里的人可以将我推向正确的方向。

I'm going to be given an input txt file which will look like this : 我将得到一个输入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 

And I'm supposed to use this data to crate n number of adjacency matrices representing the graphs. 我应该使用此数据来创建代表图形的n个邻接矩阵。 I then need to implement 3 methods on the data in the adjacency matrices: 然后,我需要对邻接矩阵中的数据实现3种方法:

  1. findLongestPath() which will return the longest path in the graph findLongestPath()将返回图中的最长路径
  2. findShortestPath() which will return the shortest path in the graph findShortestPath()将返回图中的最短路径
  3. totalNumberPaths() which will return distinct number of paths in graph totalNumberPaths()将返回图中的不同数量的路径

I'm having difficulty implementing the first two parts fine. 我很难很好地实现前两个部分。 This is what I have so far: 这是我到目前为止的内容:

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()

Obviously when it hits a row of 0s the shortest_path updates to 0 and doesn't work. 显然,当它达到0时,shortest_path更新为0,并且不起作用。 Plus it won't work when initialized to a 0. If I could get some pseudo code or maybe help with the longer or shorter method I'm sure I could write the opposite or maybe I'm totally off base. 另外,将其初始化为0时将不起作用。如果我可以获得一些伪代码,或者可能对较长或较短的方法有所帮助,我敢肯定我可以写相反的东西,或者我可能完全不合时宜。

Thanks for any input. 感谢您的任何投入。

Edit: 编辑:

So i figured it out. 所以我想通了。 Here's my finished code in case anyone has a similar problem and needs help. 如果有人遇到类似问题并需要帮助,这是我完成的代码。

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])

Figured it out. 弄清楚了。 Here is my final solution. 这是我的最终解决方案。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中查找小于或等于无环有向图的给定值的最长路径 - Find longest path less than or equal to given value of an acyclic, directed graph in Python 有向代码的有向无环图生成 - Directed Acyclic Graph Generation for Code 使用 BFS/DFS 寻找有向无环图中权重最大的路径 - Using BFS/DFS To Find Path With Maximum Weight in Directed Acyclic Graph 如何在图/邻接矩阵中找到返回自身的最短路径 - NetworkX - How to Find Shortest Path Back to Self in a Graph/Adjacency Matrix - NetworkX 非加权图如何做最短路径算法? - How to do shortest path algorithm for unweighted graph? 对有向无环图(一种特殊情况)进行拓扑排序的最有效算法是什么? - What's the most efficient algorithm for topologically sorting a (special case of a) directed acyclic graph? 在python中使用BFS的Unweighted-Single-Source-Shortest-Path - Unweighted-Single-Source-Shortest-Path using BFS in python 如何在Python中从大型邻接矩阵创建有向图? - How to Create a Directed Graph from Large Adjacency Matrix in Python? 获取python-igraph中有向图的邻接矩阵 - get adjacency matrix of a directed graph in python-igraph Python:将伪代码转换为代码 - Python: Converting Pseudo-code to code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM