INFS 519 - November 22, 2011

Table of Contents

Reading

This lecture covers material from Chapter 14.

Graphs

A graph is a nonlinear data structure with nodes and links, like a tree. Aside from being important data structures for modeling real-world data or implementing algorithms, graphs are extremely important in theoretical computer science for understanding computational complexity.

When discussing graphs, it is more common to use the term vertex instead of node and edge instead of link.

A graph is defined by a set of vertices and a set of edges.

G = ({a, b, c, d, e}, {(a, b), (a, c), (c, b), (a, d), (d, e)})

Types of Graphs

Empty graph

A graph with no vertices and no edges. G = ({}, {})

Undirected graph

A graph with undirected edges.

Directed graph

A graph with directed edges.

(See examples on board.)

More Graph Terms

Label

The name of a vertex.

Loop

A loop is an edge where the source and destination vertices are the same.

Path

A sequence of vertices in the graph such that each is connected to the next by an edge.

Multiple edges

Multiple edges connecting the same two vertices.

Simple graph

A graph with no loops or multiple edges.

Graph Implementations

There are two common ways for representing graphs: the adjacency matrix and the edge list.

An adjacency matrix is a n x n matrix of all vertices, where the cell value indicates the presence of an edge between the ith and jth vertices. An adjacency matrix is efficient for representing graphs with many connections, but less so for sparse graphs.

(See board for matrix example.)

An edge list can be used to store edges between a vertex and any other in the graph. For example, the vertex a may be connected b and c and b is connected to a, c, and d represented as a list:

edgelist(a) = (b, c) edgelist(b) = (a, c, d) edgelist(c) = (a, b) edgelist(d) = (b)

Note that the semantics are different if a graph is directed vs. undirected.

Graph Traversal

Like trees, a graph can be traversed depth-first or breadth-first.

Depth-first Search

Recursively visit the vertex connected to the current vertex, marking visited vertices throughout the traversal.

(See example, p. 722-726.)

Breadth-first Search

Visit all the vertices connected to the current vertex before proceeding to the vertices connected to those you've visited.

Use a queue to keep track of the frontier and the vertices that you should go back and explore.

(See example, p. 726-727.)

Path Algorithms

Graphs can be used to represent many kinds of problems when a solution can be expressed as a series of transitions from vertex to vertex. A breadth-first or depth-first search can be used to determine if a path exists between two vertices. Simply knowing that a path between vertices exists may be unsatisfactory for some problems, we may instead really want the shortest path.

Finding the shortest path is equivalent to finding the path with the lowest weight or cost. Weights can be incorporated into a graph by using weighted edges. Every edge then includes a cost value which should be taken into account when calculating a shortest path.

In real world terms, we could represent a travel problem as a graph where vertices represent airports and edges are flights between them. Flights will have different costs which would then be used as edge weight.

(See example on p. 733.)

Shortest Distance Algorithm

(See outline and example on p. 732-743)

The previous shortest path algorithm only provides the cost of the shortest path, not the actual sequence of vertices and edges which define that path.

Dijkstra's Algorithm

Dijkstra's algorithm is a shortest path algorithm that is similar to the shortest path cost algorithm but with the addition of a predecessor array that is used to track which vertex was used to obtain the least expensive transition.

(See details on p. 743.)

Org version 7.7 with Emacs version 23

Validate XHTML 1.0