Graphs via Adjacency Matrices
Chapter 20
Intro
Königsberg Bridge Problem
The river Pregel flows around the island of Kneiphof and then divides into two branches.


Starting at one land area, can you cross all 7 bridges exactly once and return to the start?
In 1736, Euler invented graph theory by representing this problem as a graph to solve the problem.
Euler determined that only 0 or 2 vertices (lands) can have an odd number of edges (bridges). All 4 lands have an odd number of bridges, so there is no solution.
Graph Theory
Over the past 250 years, graph theory has been applied to a variety of problems, including:
modeling and analysis of electrical circuits,
chemical compounds,
highway maps,
finding the shortest route,
project planning,
linguistics,
genetics,
social science,
etc.
Set Theory Notation
Member (
): is an element of the set . Subset (
): every element of that is an element of . Intersection (
): contains all the elements in and . and Union (
): set of all the elements that are in or in . or Ordered Pairs (
): set of all the ordered pairs of elements of and .
Graph Definitions and Notations
Graph
is a finite nonempty set of vertices of . .
Elements inare the pairs of elements of . is the set of edges.
Directed Graphs
Directed graph or digraph: elements of
- Pairs (
, ) and ( , ) represent different edges.
Undirected Graphs
Undirected graph: elements are not ordered pairs.
- Pairs (
, ) and ( , ) represent the same edge.
- Pairs (
Graph Definitions and Notations
Subgraph
of : if and . - Every vertex and edge of
is in .
- Every vertex and edge of
Adjacent: there is an edge from one vertex to the other; i.e.,
Loop: edge to and from a single vertex,
Parallel edges: associated with the same pair of vertices.
Simple graph: has no loops or parallel edges
Connected vertices: there is a path from
to . Path from
to is if there is sequence of vertices such that , , and is an edge for all . Simple path: path in which all vertices, except possibly the first and last, are distinct.
Cycle: simple path in which the first and last vertices are the same.
Connected: paths exist from each vertex to all other vertices.
If there is an edge from
to (i.e., ), then is adjacent to and is adjacent from . Strongly connected: any two vertices in
are connected.
Graph Data Structures
To process and manipulate graphs, we must store graphs in computer memory.
A graph is commonly represented in one of two ways:
Adjacency matrices
Adjacency lists
Adjacency Matrices
Given a graph,
the adjacency matrix,
Undirected Example
[0] | [1] | [2] | [3] | [4] | |
|---|---|---|---|---|---|
[0] | 0 | 1 | 1 | 0 | 0 |
[1] | 1 | 0 | 1 | 1 | 1 |
[2] | 1 | 1 | 0 | 1 | 1 |
[3] | 0 | 1 | 1 | 1 | 0 |
[4] | 0 | 1 | 1 | 0 | 0 |
The adjacency matrix of an undirected graph is symmetric.
Adjacency Matrix: Directed Example
[0] | [1] | [2] | [3] | [4] | |
|---|---|---|---|---|---|
[0] | 0 | 1 | 1 | 0 | 0 |
[1] | 0 | 0 | 1 | 1 | 1 |
[2] | 1 | 0 | 0 | 1 | 1 |
[3] | 0 | 0 | 0 | 1 | 0 |
[4] | 0 | 0 | 0 | 0 | 0 |
The adjacency matrix of a directed graph is asymmetric.
Adjacency Matrix: Weighted Directed Example
If the edges have weights, they are stored in the matrix.
Operations Commonly Performed on a Graph
Create the graph.
Clear the graph (make the graph empty).
Determine whether the graph is empty.
Traverse the graph.
Visualize the graph.
Modify the graph.
Graph Traversals
Traversing a graph is like traversing a binary tree,
except that:A graph may have cycles.
May not be able to traverse the entire graph from a single vertex.
Most common graph traversal algorithms
Depth-first traversal
Breadth-first traversal
Depth-First Traversal
Recursive depth-first traversal algorithm at a given node,
Mark node
as visited. Visit the node.
For each vertex
adjacent to ,
Ifis not visited,
begin the depth-first traversal at.
Depth-First Traversal: Example
Depth-first ordering of vertices (starting at 0):0, 1, 4, 3, 2, 5, 7, 8
Breadth-First Traversal of a Graph
Like traversing a binary tree, level by level.
Nodes at each level are visited one after another.
Use a queue to implement the breadth-first search algorithm.
Breadth-First Traversal: Example
Breadth-first ordering of vertices (starting at 0):0, 1, 3, 4, 2, 5, 7, 8
Shortest Path in a Weighted Graph
In a weighted graph, every edge has a nonnegative weight.
The weight of the path,
, is the sum of the weights of all edges on the path . The source is the first vertex of a path.
The shortest path is the path with the smallest weight.
Shortest Path Algorithm
We will implement the greedy algorithm, developed by Dijkstra.
Initialize an array,
smallestWeight, all infinity andweightFoundtofalse.Set
smallestWeight[start]to0andcurrentVertextostart.Find the vertex,
v, that is closest to thecurrentVertexfor which the shortest path has not been determined.Mark
vas the (next) vertex for which the smallest weight is found.For each vertex
winG, such that the shortest path fromstarttowhas not been determined and an edge (v,w) exists, if the weight of the path towviavis smaller than the current weight, update the weight ofwto the weight ofvplus the weight of the edge (v,w).
Given
Shortest Path Algorithm: Example
Shortest Path Algorithm: Enhancements
This demonstrated algorithm keeps track of the total weight of the shortest path. Consider what you would add to record the actual path.
Instead of using a sequence search to find the minimum weight, which is
Lab 22: Graphs via Adjacency Matrix
Let’s look at the lab based on today’s lecture.