Skip to content

Latest commit

 

History

History
57 lines (48 loc) · 2.51 KB

File metadata and controls

57 lines (48 loc) · 2.51 KB

Graph basic

Simple Graphs: have no loops and no multiple edges

Depth-first traversal (stack or recursion): visit next neighbor only after traversing all possible edges from the current neighbor and also its neighbors, until the end.
Breadth-first traversal (queue): Visit all neighbors before visiting any of their neighbors (order does not matter).

Find the shortest path

The shortest (minimal) path has the lowest cost, not the fewest edges. There could be more than one shortest path.

Greedy Algorithms: Dijkstra’s Algorithm

Not only record the shortest distance, but also the path by storing the previous node.
image
image
image

Greedy Algorithms: Prim’s Algorithm

Sudo code:

MST-Prim(G,w,r)
  Q = V[G]
  foreach u in Q
      do: key[u] = ? // initialize to ‘infinity’
  key[r] = 0
  pi[r] = null
  while Q is not empty
      do: u = ExtractMin(Q)  
            // find light edge; u = r first time through
            foreach v in Adj[u]
                 do: if v in Q && w(u,v) < key[v] 
                          // update adjacent nodes
                          then pi[v] = u
                                  key[v] = w(u,v)

image

One Dynamic Programming: Floyd Warshall

image

Sudo code:

for each vertex u in G do
   for each vertex v in G do
      cost[u,v] = c[u,v]  
for each vertex w in G do  
    for each vertex u in G do
       for each vertex v in G do
         cost[u,v] = min(cost[u,v],
                   cost[u,w] + cost[w,v]

Reference video: https://www.youtube.com/watch?v=4OQeCuLYj-4

image
image
image
image