返回

用python解析生成邻接表,构建有向图的反向邻接表

人工智能

用python解析生成邻接表

邻接表是一种用来表示图的数据结构。在邻接表中,每个顶点都有一个链表,链表中的每个节点都存储着与该顶点相邻的另一个顶点。

为了生成邻接表,我们需要先创建一个空的列表。然后,对于图中的每个顶点,我们创建一个新的链表并将其添加到列表中。最后,对于图中的每条边,我们在两个顶点的链表中都添加一个新的节点,指向另一个顶点。

def create_adjacency_list(num_vertices, edges):
    """
    Create an adjacency list representation of a directed graph.

    Args:
        num_vertices: The number of vertices in the graph.
        edges: A list of tuples representing the edges in the graph. Each tuple
            contains two integers, representing the head and tail of the edge.

    Returns:
        A list of lists, where each inner list represents the vertices that are
        adjacent to the corresponding vertex in the graph.
    """

    # Create an empty list to store the adjacency list.
    adj_list = [[] for _ in range(num_vertices)]

    # For each edge in the graph, add a new node to the adjacency list of the
    # head and tail vertices.
    for edge in edges:
        head, tail = edge
        adj_list[head].append(tail)

    return adj_list


if __name__ == "__main__":
    # Create a directed graph with 5 vertices and 6 edges.
    num_vertices = 5
    edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (1, 3)]

    # Generate the adjacency list representation of the graph.
    adj_list = create_adjacency_list(num_vertices, edges)

    # Print the adjacency list.
    for i, adj_vertices in enumerate(adj_list):
        print(f"Vertex {i}: {adj_vertices}")

构建有向图的反向邻接表

有向图的反向邻接表与邻接表非常相似,但它存储的是从每个顶点出发而不是到每个顶点的边。换句话说,反向邻接表中的每个链表都存储着与该顶点相邻的所有其他顶点。

为了构建有向图的反向邻接表,我们可以使用与生成邻接表相同的方法。唯一的区别是,对于图中的每条边,我们在两个顶点的链表中添加的节点是反向的。

def create_reverse_adjacency_list(num_vertices, edges):
    """
    Create a reverse adjacency list representation of a directed graph.

    Args:
        num_vertices: The number of vertices in the graph.
        edges: A list of tuples representing the edges in the graph. Each tuple
            contains two integers, representing the head and tail of the edge.

    Returns:
        A list of lists, where each inner list represents the vertices that are
        adjacent to the corresponding vertex in the graph.
    """

    # Create an empty list to store the reverse adjacency list.
    reverse_adj_list = [[] for _ in range(num_vertices)]

    # For each edge in the graph, add a new node to the reverse adjacency list
    # of the tail and head vertices.
    for edge in edges:
        head, tail = edge
        reverse_adj_list[tail].append(head)

    return reverse_adj_list


if __name__ == "__main__":
    # Create a directed graph with 5 vertices and 6 edges.
    num_vertices = 5
    edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (1, 3)]

    # Generate the reverse adjacency list representation of the graph.
    reverse_adj_list = create_reverse_adjacency_list(num_vertices, edges)

    # Print the reverse adjacency list.
    for i, adj_vertices in enumerate(reverse_adj_list):
        print(f"Vertex {i}: {adj_vertices}")

总结

在本文中,我们学习了如何用python解析生成邻接表,以及如何构建有向图的反向邻接表。这些知识在图算法中非常有用,可以帮助我们解决许多实际问题。