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

can also be done by DP

微信图片_20190506110907.jpg
微信图片_201905061109071.jpg
微信图片_201905061109072.jpg
微信图片_201905061109073.jpg
微信图片_201905061109074.jpg
微信图片_201905061109075.jpg

你可能感兴趣的:(2019-04-24 Graphs)