Computer Algorithms II Lecture Notes

6 November 2008 • Graph Basics


Outline

Route Finding

Traffic Scheduling

Schematic Analysis

Graphs

The Königsberg Bridges

The Bridges Problem

Graph Terminology

Graph Labels

Graph Models

Graph Data Structures

Graph Traversal

More Graph Terms

a directed edge

Paths

Adjacency

Basic Traversal

One-Visit Assumption

Random Traversal

rt(graph g, vertex current)

  set pending-visits(current)

  while not pending-visits.empty()
    current = pending-visits.pick()
    current.visited = true
    for each n in g.neighbors(current)
      if n not in pending-visits 
         and not n.visited
	pending-visits.add(n)

Graph Traversal Properties

Graph Algorithm Performance

Random Traversal Peferformance

set pending(current)          O(1)
while not pending.empty()     O(1)×O(|V|)
  current = pending.pick()    O(1)
  nbrs = g.neighbors(current) O(|V|)
  for each n in nbrs          O(1)×O(|V|)
    if not n.visited          O(1)
      n.visited = true        O(1)
      pending.add(n)          O(log |E|)

A Simple Problem

Ordered Backtracking

Breadth-First Traversal

vertex bft(graph g, vertex current)

  queue pending-visits(current)

  while not pending-visits.empty()

    current = pending-visits.deq()
    current.visited = true

    for each n in g.neighbors(current)
      if p(n) return n
      if n not in pending-visits 
         and not n.visited
	pending-visits.enq(n)

Cycles

Another Simple Problem

Depth-First Traversal

bool dft(graph g, vertex current)

  stack pending-visits.push(current)

  while not pending-visits.empty()

    current = pending-visits.pop()
    current.visited = true

    for each n in g.neighbors(current)
      if n in pending-visits or n.visited
        return true
      pending-visits.push(n)

  return false

Graph Data Structures

Adjacency Matrix

Edge Lists

Vertex Lists

Graph Data Structures

Summary


This page last modified on 4 December 2007.

Creative
    Commons License