Python 从文件中读取有向图

Python 从文件中读取有向图,python,discrete-mathematics,digraphs,lab,Python,Discrete Mathematics,Digraphs,Lab,大家好,如果有人想知道的话,我对编码是新手 这是我第一次在这里发帖,我目前正忙于一项任务。下面的代码是我的草稿代码 邻接列表的预期输出为: [[1,3], [2], [3], [0,2]] # with possibly [3,1] instead of [1,3] or [0,2] instead of [2,0] 我的代码的输出是: [[1,3],[2],[3],[0,2]]——这正是我想要的 对于的预期输出,最大路径为: [[1,3], [2], [3], [0,2]] # with p

大家好,如果有人想知道的话,我对编码是新手

这是我第一次在这里发帖,我目前正忙于一项任务。下面的代码是我的草稿代码

邻接列表的预期输出为:

[[1,3], [2], [3], [0,2]]
# with possibly [3,1] instead of [1,3] or [0,2] instead of [2,0]
我的代码的输出是:

[[1,3],[2],[3],[0,2]]
——这正是我想要的

对于
的预期输出,最大路径为:

[[1,3], [2], [3], [0,2]]
# with possibly [3,1] instead of [1,3] or [0,2] instead of [2,0]
[0,1,2,3]
[0,3,2]

鉴于我的输出为:

[0,1,3,2]
——这与我教授想要的预期结果完全不同。我一直在执行第二个函数,但一直遇到死胡同。有人能告诉我第二个函数的错误在哪里吗

以下是digraph.txt列表:

6 4
0 1
0 3
1 2
2 3
3 0
3 2

谢谢大家!

def adjacency_list(file_name):

    # create empty list
    adj_temp_list = []

    # open input file
    f = open('digraph0.txt','r')

    # read first line
    in_file = f.readline()

    first_row = [int(i) for i in in_file.strip().split()]

    # first_row[0] represents number of edges and first_row[1] represents number of vertices

    # add number of vertices amount of empty lists to adj_temp_list
    for i in range(first_row[1]):

        adj_temp_list.append([])

    # read edges( each line represents one edge)
    for i in range(first_row[0]):
        line = f.readline()

        # split the line and convert the returned list into a list of integers
        vertex=[int(i) for i in line.strip().split()]

        # vertex[0] and vertex[1] represents start vertex and end vertex respectively.
        # that is, vertex[1] is adjacent vertex or neighbour to vertex[0]
        # Thus, append vertex[1] to the adjcency list of vertex[0]

        adj_temp_list[vertex[0]].append(vertex[1])

    # close input file
    f.close()

    # create a new empty list
    adj_list=[]

    # sort each list in the adj_temp_list and append to adj_list
    for lst in adj_temp_list:
        lst.sort()
        adj_list.append(lst)

    # return the adjacency list
    return adj_list

def maximal_path(file_name,start):

    # call adjacency_list function to get adjacency list
    adj_list=adjacency_list(file_name)

    # create max_path as an empty list
    max_path=[]

    # create a boolean list that represents which vertex is added to path
    # Thus, initialize the list such that it represents no vertex is added to path
    visited=[False]*len(adj_list)

    # create an empty list(stack)
    stack=[]

    # add start vertex to stack list
    stack.append(start)

    # process until there is no possibility to extend the path
    while True:

    # get a node u from stack and remove it from stack
        u=stack[0]
        stack=stack[1:]

    # set vertex u visited and add it to max_path
        visited[u]=True
        max_path.append(u)

    # find whether u has unvisited neighbours to extend path or not
        existNeighbour=False
        for v in adj_list[u]:
            if visited[v]==False:
                existNeighbour=True
                break

    # if u has unvisited adjacent vertices
        if existNeighbour==True:

        # push all un-visited neighbours into stack
            for v in adj_list[u]:
                stack.append(v)

    # if u has no unvisited adjacent vertices, exit the loop
        else:
            break

# return the maximal path
    return max_path

你的方法被称为a或BFS。将新节点附加到堆栈末尾时,将逐层浏览图形。为了获得预期的输出,您可以对程序进行的最小更改是在堆栈的开头插入新节点

stack.insert(0,v)

你的方法被称为a或BFS。将新节点附加到堆栈末尾时,将逐层浏览图形。为了获得预期的输出,您可以对程序进行的最小更改是在堆栈的开头插入新节点

stack.insert(0,v)

你的问题名为“从文件中读取有向图”,但你向我们展示了两个函数,一个是从文件中读取的函数,另一个是有问题的,我认为没有必要发布这两个函数,因为你的问题只是其中之一。尝试将其编辑为一个。预期的输出如何同时具有两个不同的响应?你是说“或”而不是“和”?你的问题题为“从文件中读取有向图”,但你向我们展示了两个函数,说从文件中读取的函数是正确的,而另一个函数有问题-我认为没有必要发布这两个函数,因为你的问题只是其中之一。尝试将其编辑为一个。预期的输出如何同时具有两个不同的响应?你是说“或”而不是“和”?