Graphs
Graphs
Learning Outcomes
2
Lecture Scope
Graph terminology
Applications
Undirected graph
Directed graph
Weighted graph
Graph traversals
Shortest path
Dijkstra algorithm
Graph representation 3
Graphs - Terminology
Like trees, graphs are made up of nodes and the connections between
those nodes
Edges are referenced by a pairing of the vertices (A, B) that they connect
4
Graphs - Terminology (cont…)
The length of a path in is the number of edges in the path (or the
number of vertices minus 1)
5
Graphs - Terminology (cont…)
6
Graphs - Terminology (cont…)
7
Graphs - Applications
Electronic circuits
Printed circuit board
Integrated circuit
Transportation networks
Highway network
Flight network
Computer networks
Local area network cslab1a cslab1b
math.brown.edu
Internet
cs.brown.edu
Web brown.edu
qwest.net
att.net
Databases
cox.net
Entity-relationship diagram John
8
Paul
David
Graphs - Applications (cont…)
9
Undirected Graphs
An undirected graph is a graph where the pairings representing the edges
are unordered
|V| = n
Associated with this is a collection E of unordered pairs {vi, vj} termed edges
which connect the vertices
An undirected graph:
10
Undirected Graphs (cont…)
Adjacency lists
11
Undirected Graphs (cont…)
12
Undirected Graphs (cont…)
E = {{v1, v2}, {v3, v5}, {v4, v8}, {v4, v9}, {v6, v9}}
The pair {vj , vk} indicates that both vertex vj is adjacent to vertex vk
and vertex vk is adjacent to vertex vj
13
Undirected Graphs (cont…)
14
Undirected Graphs - Path (cont…)
15
Undirected Graphs - Path (cont…)
A path of length 4:
(A, B, E, C, F)
A path of length 5:
(A, B, E, C, B, D)
16
Undirected Graphs - Degree
degree(B) = degree(E) = 4
degree(F) = 1
degree(G) = 0
Two vertices vi, vj are said to be connected if there exists a path from vi to vj
19
Directed Graphs
20
Directed Graphs (cont…)
edges (A, B), (A, D), (B, C), (B, D) and (D, C)
22
Directed Graphs (cont…)
E = {(v1, v2), (v3, v5), (v5, v3), (v6, v9), (v8, v4), (v9, v4)}
23
Directed Graphs - Paths
24
Directed Graphs - In and Out Degrees
In this graph:
in_degree(v1) = 0 out_degree(v1) = 2
in_degree(v5) = 2 out_degree(v5) = 3
25
Directed Graphs - Sources and Sinks
Some definitions:
In this graph:
Sinks: v2, v9
26
Directed Graphs - Connectedness
27
Directed Graphs - Connectedness (cont…)
Strongly Connected
Two vertices vj, vk are said to be connected if there
exists a path from vj to vk
A graph is strongly connected if there exists a directed
path between any two vertices
Weakly Connected
In this graph:
The sub-graph {v3, v4, v5, v8} is strongly
connected
29
Weighted Graphs
The weight of a path in a weighted graph is the sum of the weights of the
edges in the path
For weighted graphs, we represent each edge with a triple including the
starting vertex, ending vertex, and the weight (Boston, New York, 120)
30
Weighted Graphs (cont…)
31
Weighted Graphs (cont…)
Unlike weighted undirected graphs, if both (vj, vk) and (vj, vk) are
edges, it is not required that they have the same weight
6.4
32
Weighted Graphs (cont…)
33
Weighted Graphs (cont…)
34
Weighted Graphs (cont…)
The length of a path within a weighted graph is the sum of all of the
edges which make up the path
The length of the path (A, D, G) in the following graph is 5.1 + 3.7 = 8.8
35
Weighted Graphs (cont…)
36
Weighted Graphs (cont…)
37
Implementing Graphs
Adjacency lists
use a set of graph nodes which contain a linked list storing the edges within
each node
for weighted graphs, each edge would be stored as a triple including the
weight
Adjacency matrices
use a two dimensional array
Graph
ADT (general
concept)
39
Graph Representation
a b c a b c d e f
a 0 0 0 0 1 0
adjacency matrix
d e f b 0 0 0 1 0 1
c 0 0 0 0 0 0
a e d 0 1 0 0 1 1
e 1 0 0 1 0 0
b d f
f 0 1 0 1 0 0
c
ae bd bf de df
a 1 0 0 0 0
d b e f
incidence matrix
b 0 1 1 0 0
e a d c 0 0 0 0 0
d 0 1 0 1 1
f b d e 1 0 0 1 0
f 0 0 1 0 1 40
adjacency list
Adjacency Matrix Representation
41
Adjacency Matrix Representation (cont…)
42
Adjacency Matrix Representation (cont…)
43
Adjacency Matrix Representation (cont…)
1 2 3 4 5 6 7 8 9
1 T T
2
3 T
4 T T
5 T T T
6 T
7 T
8 T
9
44
Adjacency List Representation
This involves representing the set of vertices adjacent to each vertex as
a list. Thus, generating a set of lists.
Edges as an array of linked list (the emanating edges of vertex 1 will be in the
list of the first element, and so on, …
vertices edges
3 Empty
1 •→2→4
2 •
3 •→5
4 •→2→5
5 •→2→3→8
6 •→9
7 •→9
8 •→4
9 •
46
Common Graph Algorithms
These include
various traversal algorithms
47
Graph Traversals
48
Breadth-First Traversal
In this method, after visiting a vertex v, we must visit all its adjacent
vertices w1, w2, w3, ..., before going down next level to visit vertices
adjacent to w1 etc.
49
Breadth-First Traversal (cont…)
We will use the queue to manage the traversal and the iterator to
build the result
51
Breadth-First Traversal (cont…)
iteration v queue
init a
1 a b c f
a(0)
(1) b (2)
(0) c (3)
(0)
2 b c f e g
3 c f e g
d(7)
(0) e (5)
(0) f (4)
(0) 4 f e g
5 e g d
6 g d h
g(6)
(0) h (8)
(0)
7 d h
8 h
52
Breadth-First Traversal (cont…)
/**
* Returns an iterator that performs a breadth first
* traversal starting at the given index.
*
* @param startIndex the index from which to begin the traversal
* @return an iterator that performs a breadth first traversal
*/
public Iterator<T> iteratorBFS(int startIndex)
{
Integer x;
QueueADT<Integer> traversalQueue = new LinkedQueue<Integer>();
UnorderedListADT<T> resultList = new ArrayUnorderedList<T>();
if (!indexIsValid(startIndex))
return resultList.iterator();
traversalQueue.enqueue(new Integer(startIndex));
visited[startIndex] = true;
53
Breadth-First Traversal (cont…)
while (!traversalQueue.isEmpty())
{
x = traversalQueue.dequeue();
resultList.addToRear(vertices[x.intValue()]);
54
Depth-First Traversal
Otherwise, go back to the most previous vertex that has not yet had all
of its adjacent vertices visited and continue from there
Two implementations:
Recursive
Iterative
56
Depth-First Traversal (cont…)
/**
* Returns an iterator that performs a depth first traversal
* starting at the given index.
*
* @param startIndex the index from which to begin the traversal
* @return an iterator that performs a depth first traversal
*/
public Iterator<T> iteratorDFS(int startIndex)
{
Integer x;
boolean found;
StackADT<Integer> traversalStack = new LinkedStack<Integer>();
UnorderedListADT<T> resultList = new ArrayUnorderedList<T>();
boolean[] visited = new boolean[numVertices];
if (!indexIsValid(startIndex))
return resultList.iterator();
traversalStack.push(new Integer(startIndex));
resultList.addToRear(vertices[startIndex]);
visited[startIndex] = true; 57
Depth-First Traversal (cont…)
while (!traversalStack.isEmpty())
{
x = traversalStack.peek();
found = false;
59
Graph Traversals: Example (cont...)
A
60
Graph Traversals: Example (cont...)
B C E
61
Graph Traversals: Example (cont...)
A, B
C E D
62
Graph Traversals: Example (cont...)
A, B, C
E D F
63
Graph Traversals: Example (cont...)
A, B, C, E
D F G H
64
Graph Traversals: Example (cont...)
Dequeue D
A, B, C, E, D
F G H
65
Graph Traversals: Example (cont...)
Dequeue F
A, B, C, E, D, F
G H
66
Graph Traversals: Example (cont...)
A, B, C, E, D, F, G
H I
67
Graph Traversals: Example (cont...)
Dequeue H
A, B, C, E, D, F, G, H
I
68
Graph Traversals: Example (cont...)
Dequeue I
A, B, C, E, D, F, G, H, I
69
Graph Traversals: Example (cont...)
A, B, C, E, D, F, G, H, I
70
Graph Traversals: Example (cont...)
71
Graph Traversals: Example (cont...)
72
Graph Traversals: Example (cont...)
A, B
73
Graph Traversals: Example (cont...)
A, B, C
74
Graph Traversals: Example (cont...)
A, B, C, D
75
Graph Traversals: Example (cont...)
A, B, C, D, E
76
Graph Traversals: Example (cont...)
A, B, C, D, E, G
77
Graph Traversals: Example (cont...)
A, B, C, D, E, G, I
78
Graph Traversals: Example (cont...)
A, B, C, D, E, G, I, H
79
Graph Traversals: Example (cont...)
A, B, C, D, E, G, I, H, F
80
Graph Traversals: Example (cont...)
A, B, C, D, E, G, I, H, F
81
Shortest Path
82
Shortest Path (cont…)
83
Shortest Path - Algorithms
A* search algorithm
Bellman-Ford algorithm
…
84
Dijkstra’s Algorithm
The goal of this algorithm will be to find the shortest path and its length
It finds the shortest path from an initial vertex, say s, to all the other
vertices.
We will make the assumption that the weights on all edges is a positive
number
Clearly, if we have negative vertices, it may be possible to end up in a cycle
whereby each pass through the cycle decreases the total length
85
Dijkstra’s Algorithm - Algorithm Steps
86
Dijkstra’s algorithm - Example 1
87
Dijkstra’s algorithm - Example 1 (cont…)
88
Dijkstra’s algorithm - Example 1 (cont…)
89
Dijkstra’s algorithm - Example 1 (cont…)
90
Dijkstra’s algorithm - Example 1 (cont…)
91
Dijkstra’s algorithm - Example 1 (cont…)
92
Dijkstra’s algorithm - Example 1 (cont…)
93
Dijkstra’s algorithm - Example 1 (cont…)
94
Dijkstra’s algorithm - Example 2
95
Running Time of Some Graph Algorithms
96
Summary
A path is a sequence of edges that connects two vertices in a graph.
A cycle is a path in which the first and last vertices are the same and none of the edges are repeated.
An undirected graph is a graph where the pairings representing the edges are unordered.
A directed graph, sometimes referred to as a digraph, is a graph where the edges are ordered pairs of
vertices.
A weighted graph, sometimes called a network, is a graph with weights (or costs) associated with
each edge.
An undirected graph is considered connected if for any two vertices in the graph there is a path
between them.
97
Summary (cont…)
Path and cycles in a digraph: must move in the direction specified by the arrow.
Weakly connected: If the underlying undirected graph is connected (i.e. ignoring the arrows).
The length of a path within a weighted graph is the sum of all of the edges which make up the path.
There are two main types of graph traversal algorithms: breadth-first (requires a queue), and
depth-first (requires a stack).
Given a weighted directed graph, we can find the shortest distance between two vertices by:
growing this path by always going to the next vertex which has the shortest current path
Dijkstra's algorithm solves the single-source shortest path problem for a non-negative weights
graph.
98
References
Data Structures and Problem Solving Using Java, 4th edition, Weiss.
99
Any Question
???
100