Advanced Programming I Lecture Notes

21 March 2006 • Graph Basics


Outline

Route Finding

Traffic Scheduling

Schematic Analysis

Graphs

The Königsberg Bridges

The Bridges Problem

Graph Terminology

Graph Labels

Graph Models

Graph ADTs

Graph Traversal

More Graph Terms

a directed edge

Paths

Adjacency

Basic Traversal

One-Visit Assumption

Graph Traversal

dt(graph g, vertex current)
  current.visiting = true
  set pending(current)
  while not pending.empty()
    current = pending.pick()
    current.visited = true
    for each n in g.neighbors(current)
      if not n.visited
        n.visited = true
	pending.add(n)

Graph Traversal Properties

Graph Algorithm Performance

Graph 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.enq(current)
  while not pending.empty()
    current = pending.deq()
    current.visited = true
    for each v in g.neighbors(current)
      if p(v) return v
      if not v.visited
        v.visited = true
	pending.enq(v)

Cycles

Another Simple Problem

Depth-First Traversal

bool
dft(graph g, vertex current)
  stack pending.push(current)
  while not pending.empty()
    current = pending.pop()
    current.visited = true
    for each v in g.neighbors(current)
      if v.visited
        return true
      else
        n.visited = true
	pending.push(n)

Graph ADT

Example Graph ADTs

Adjacency Matrix

Edge Lists

Vertex Lists


This page last modified on 13 April 2006.

This work is covered by a
Creative Commons License.