js401-reading

Graphs

gist

Terminology

directed vs undirected

complte vs connected vs disconnected

Acylcic vs cyclic

graph representation

Matrix

a b c d e f a0 0 1 1 0 0 b0 0 1 0 0 1 c1 1 0 0 1 0 d1 0 0 0 1 0 e0 0 1 1 0 1 f0 1 0 0 1 0

Adjecency list

a>c>d b>c>f c>a>b>e d>a>e e>c>d>f f>b>e

Weighted Graphs

a b c d a0 4 3 9 b4 0 0 5 c3 0 0 6 d9 5 6 0

a>b,4>d,9>c,3 b>a,4>d,5 c>a,3>d,6 d>a,9>b,5>c,6

Traversals

Breadth First


  ALGORITHM BreadthFirst(vertex)
    DECLARE nodes <-- new List()
    DECLARE breadth <-- new Queue()
    breadth.Enqueue(vertex)

    while (breadth is not empty)
        DECLARE front <-- breadth.Dequeue()
        nodes.Add(front)

        for each child in front.Children
            if(child is not visited)
                child.Visited <-- true
                breadth.Enqueue(child)

    return nodes;
    

Depth First

  1. push root on to stack
  2. while loop of not empty stack
  3. peek at top node
  4. if top has unvisited children, mark top node as visited, then push children back into stack
  5. if no unvisited children, pop off stack
  6. repeat until empty

real world uses

  1. gps and mapping
  2. driving directions
  3. social networks
  4. airline traffic
  5. netflix suggestions