Week 7: Graph: Data Structures & Algorithm Analysis
Week 7: Graph: Data Structures & Algorithm Analysis
1
Lecture Outline
Graph
Graph terminology
Graph representation
Graph traversal
Depth-first traversal
Breadth-first traversal
Topological Ordering
Shortest Path
2
Learning Objective
To describe the characteristics of the graph including its
vertices, edges and path,
To differentiate between directed and undirected graph,
To describe and differentiate between adjacency matrix
and list,
To identify how to traverse nodes using breadth and
depth first search,
To identify how to find topological order in a graph, and
To find shortest path for weighted or un-weighted graph
3
Road Maps
Nodes
Edges
6
Graph Terminology (cont’s)
Hubbard & Anita (2004),
Graph is a pair G = (V, E) are sets and
every element of E is a two-element subset
of V
V – Vertices E – Edges
SE
UK DE
CZ
Size of graph = 8 FR CH AT
IT
8
Subgraph
SE
UK DE DE
CZ CZ
FR CH AT FR CH AT
IT IT
G G’
A subgraph is a portion of a graph that itself is a graph
If G = (V, E) is a graph and G’ = (V’, E’) where V’ V and E’ E,
then, G’ is a subgraph of G.
Graph G’ is a sub graph of the graph G. Two vertices (SE, UK) and
two edges have been removed. 9
Directed and Undirected Graphs
The edges of a graph are directed if the
existence of an edge from A to B does not
necessarily guarantee that there is a path
in both directions
A graph with directed edges
is called a directed graph.
The edges are drawn using arrows.
A graph with undirected edges is an
undirected graph or simply a graph. The
edges are drawn using lines.
10
Paths
A sequence of edges that connect two
vertices in a graph
The length of a path is the number of edges
that it comprises.
In a directed graph the direction of the
edges must be considered
called a directed path
A cycle is a path that begins and ends at
same vertex
Simple path : if the path does not pass through
any vertex more than once
A graph with no cycles is acyclic
11
Paths and Cycles
12
Weights
The edges in a graph may have values
associated with them known as their
weights
A graph with weighted edges is known as a
weighted graph
A weighted graph has values on its edges
Weights or costs
A path in a weighted graph also has weight
or cost
The sum of the edge weights
Examples of weights
Miles between nodes on a map
Driving time between nodes
Taxi cost between node locations 13
Weights
14
Category of Graph
Undirected Graph
Every edge has no arrows
15
Directed Graph (Digraph)
G1 = (V1, E1)
V1 = { a, b, c, d, e, f}
E1 = {(a,d), (a,e), (d,c), (e,b), (b,a), (b,c), (c,f), (f,f) }
e a d
b f
16
Undirected Graph
G2 = (V2, E2)
V2 = {a, b, c, d}
E2 = { {a,b}, {a,c}, {b,c}, {c,d} }
or
E2 = { (a,b), (b,a), (a,c), (c,a), (b,c), (c,b), (c,d), (d,c) }
Attention: a b
{a,b} = (a,b), (b,a)
c d
17
Connected Graphs
A connected graph
Has a path between every pair of
distinct vertices
A complete graph
Has an edge between every pair of
distinct vertices
A disconnected graph
Not connected
18
Connected Graphs
Undirected graphs 19
Course Prerequisites
20
Trees
All trees are graphs
But not all graphs are trees
21
Airline Routes
Note the graph with two subgraphs
Each subgraph connected
Entire graph disconnected
24
The Adjacency Matrix
Is a 2 dimensional n n matrix.
For a graph with n vertices, has n rows and
n columns
Each row, each column corresponds to a vertex
in the graph
Numbered 0 through n – 1
Element aij indicates whether an edge exists
between vertex i and vertex j
Elements of the matrix contain
Boolean for unweighted graph
Edge weights for weighted graph
25
The Adjacency Matrix
26
The Adjacency Matrix – directed graph
e a d
b f
a b c d e f
a 0 0 0 1 1 0
A1 = b 1 0 1 0 0 0
c 0 0 0 0 0 1
d 0 0 1 0 0 0
e 0 1 0 0 0 0
f 0 0 0 0 0 1
27
The Adjacency Matrix – undirected graph
a b
a b c d
c d
a 0 1 1 0
A2 = b 1 0 1 0
c 1 1 0 1
d 0 0 1 0
28
The Adjacency Matrix
29
The Adjacency List
30
The Adjacency List
Adjacency lists
for the directed
graph
31
The Adjacency List – directed graph
a
d e
b
a c
c e a d
f
d
c c
e b f
b
f
f
32
The Adjacency List – undirected graph
a b
a b c
b a c c d
c a b d
d c
33
Graph Traversal
The most common graph traversal
algorithm:
Breadth- First Traversal
It follows a path that explores an entire level before
moving to next level
The traversal uses one queue to hold the unvisited
neighbors of a vertex, and another queue to maintain
traversal order.
Depth- First Traversal
It follows a path that goes as deeply into the graph as
possible before following other graph
The traversal uses a stack to hold the unvisited
neighbors of a vertex, and a queue to maintain
traversal order 34
Breadth-First Traversal
A trace of a breadth-
first traversal for a
directed graph,
beginning at vertex
A.
35
Breadth-First Traversal
1 a
2 3 4
b c d
5
e 6 h
7 f g 8
36
Breadth-First Traversal (Trees)
37
Algorithm for Breadth-First Traversal
38
Example of a Breadth-First Traversal
39
Example for Breadth-First Traversal
40
Depth-First Traversal
Etc.
41
Depth-First Traversal
1 a
2 6 8
b c d
3
e 7 h
4 f g 5
42
Depth-First Traversal
A trace of a depth-first
traversal beginning at
vertex A of the
directed graph
43
Topological Order
A graph can have several different
topological orders (linear ordering)
Possible only for a directed graph without
cycles
A graph that has no cycle:
There is exists a vertex u in G such that u has
no predecessor
There is exists a vertex v in G such that v has
no successor
In a topological order
Vertex a precedes vertex b whenever
A directed edge exists from a to b
Can be implemented using either the dept
first traversal or the breadth first traversal. 44
Topological
Order
Three
topological
orders.
45
Topological Order
Algorithm for Breadth First Topological Ordering
Create an array predCount, and initialize it so
that predCount[i] is the number of
predecessors of the vertex vi.
Initialize the queue, say queue, to all those
vertices vk so that predCount[k] is zero.
While the queue is not empty:
Remove the front element, u, of the queue
Put u in the next available position, say
topologyOrder[topIndex], and increment
topIndex
For all the immediate successors w of u:
Decrement the predecessor count of w by 1.
If the predecessor count of w is zero, add w to queue
46
Topological Order
47
Shortest Path in a Graph
A graph can have several different paths
between the same two vertices.
In unweighted, the shortest path is a
path with shortest length or a path that
has fewest edges.
In weighted, the shortest path is a path
that has the smallest edge-weight sum.
48
Shortest Path in an Unweighted Graph
49
Shortest Path in an Unweighted Graph
51
Shortest Path in an Unweighted Graph
53
Shortest Path in an Weighted Graph
54
Shortest Path in an Weighted Graph
Finding the
cheapest path
from vertex A to
vertex H in the
weighted graph in
Fig (a).
55
Shortest Path in an Weighted Graph
56
Applications of Graph
57
References
Data Structures and Abstractions with Java . Authors: Frank
M. Carrano & Walter Savitch . Chapter 29 & 30
58