2019-04-24 Graphs

Directed and non-directed graphs

Directed

Graph is strongly connected if there is a path fro any vertex to any other vertex
directed

indegree = # ingoing edges
outdegree = # outgoing edges
sum are equal

non-directed

Graph is connected if there is a path between any two vertices
non-directed

Degree of v is # neighbors
sum of degree = 2 * #edges

adjacency lists
adjacency matrix

BFS

Input

Graph G given by AL
vertex v in G

Output

Array dist[1,n], where dist[u] is the length of the shortest path from v to u

function BTS(v;;G[1..n])
    dist[1..n] filled with inf
    dist[v] = 0
    queue Q empty
    push v to Q
    while Q not empty
        u = Q.pop()
        for w in G[u]
            if dist[w] = inf
                dist[w] = dist[u] + 1
                push w to Q
  return dist

\sum_u{(O(1)+O(outdegree\ u))} = O(n)+O(m)=O(n+m)

can also be done by DP

微信图片_20190506110907.jpg
微信图片_201905061109071.jpg
微信图片_201905061109072.jpg
微信图片_201905061109073.jpg
微信图片_201905061109074.jpg
微信图片_201905061109075.jpg
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容