Dsa24 8
Dsa24 8
Graphs
Main content of last lecture
§ Basic tree concepts: general tree Hard to implement
Example of graphs
6
Directed graphs
V(G) = {1,2,3,4,5}
E(G)={(1,2), (2,1), (1,5), (5,1), (5,2), (2,5), (2,3), (3,2),
(2,4), (4,2), (3,4), (4,3), (4,5), (5,4)}
1, if (i, j) Î E
Adj(i, j) =
0, otherwise
Adjacency matrix
Typically contain data with with key
Adjacency
matrix
16
Hold data
Adjacency list:
auxiliary data
structures:
list and stack
21
auxiliary data
structures:
list and queue
23
a y h a v e m u ltiple
A g ra p h m m u ltiple
e e s a n d
spanning tr tr ees
m s p a n n in g
minimu
Spanning Tree in a graph
Source Vertex: A
Edges Weight
(B, C) 2
(C, A) 3
(D, C) 3
(E, D) 2
(F, D) 3
31
//Initialisation
for (int j = 0; j < gSize; j++) {
mstv[j] = false;
edges[j] = source;
edgeWeights[j] = weights[source][j];
}
32
Shortest paths
§ Applications of finding shortest paths
§ transport,
§ navigation
§ communication
§ image segmentation
§ speech processing
§ graph drawing
§ …
Shortest path in a graph
§ Two types of shortest path problems:
§ Single source to all destinations
§ The shortest path between any pair of locations
§ The most famous algorithm for single source to all
destinations is known as Dijkstra’s algorithm ( O(n2) )
§ Applies to both directed and undirected graphs
Determine
shortest paths
Shortest Path…
Determine
shortest path
39
Assignment 2
Location Price
1 Penrith $0.25
2 Kingswood $0.00
3 Mount Druitt $0.00
4 Rooty hill $0.40
5 Blacktown $0.00
6 Parramatta $0.45
7 Olympic Park $0.60
8 Burwood $0.49
9 Colebee $0.49
10 Bedmond $0.30
11 Baulkham Hills $0.00
12 West Pennant Hills $0.49
13 Macquarie Pk $0.30
14 North Syndney $0.50
15 Central $0.55
16 Mascot $0.33
17 Kingsgrove $0.58
18 Bankstown $0.50
19 Liverpool $0.49
20 Campbelltown $0.53
43
Reading
§ Read textbook Chapter 12
§ Download example code from vUWS and read it, which is
extremely import for assignment 2.
§ Assignment 2 is an application to graph. All the
algorithms are existing, either provided by the lectures or
accessible from the Internet. You need to fully understand
them to apply these algorithms, which takes time. Once
you understand them, coding becomes easy. Practicals 7 &
8 contains some training for the algorithms.