Graph Implementations & Shortest Path Algorithm: Matrix, List, Set/Map - Prof. Nelson Padu, Study notes of Computer Science

An in-depth analysis of various graph implementations, including adjacency matrix, adjacency list, and adjacency set/map. It covers their representations, space requirements, and time requirements. Additionally, it discusses the concept of graph density and the choice between neighbor-based and connection-based algorithms. Lastly, it introduces dijkstra's algorithm for finding the shortest path in a graph.

Typology: Study notes

Pre 2010

Uploaded on 07/29/2009

koofers-user-y4w-1
koofers-user-y4w-1 🇺🇸

9 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
CMSC 132:
Object-Oriented Programming II
Graph Implementations &
Single Source Shortest Path
Algorithm
Department of Computer Science
University of Maryland, College Park
2
Graph Implementation
How do we represent edges?
Adjacency matrix
2D array of neighbors
Adjacency list
List of neighbors
Adjacency set / map
Set / map of neighbors
Important for very large graphs
Affects efficiency / storage
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Graph Implementations & Shortest Path Algorithm: Matrix, List, Set/Map - Prof. Nelson Padu and more Study notes Computer Science in PDF only on Docsity!

1

CMSC 132:

Object-Oriented Programming II

Graph Implementations &

Single Source Shortest Path

Algorithm

Department of Computer Science

University of Maryland, College Park

2

Graph Implementation

How do we represent edges?

Adjacency matrix 2D array of neighbors Adjacency list List of neighbors Adjacency set / map Set / map of neighbors

Important for very large graphs

Affects efficiency / storage

3

Adjacency Matrix

Representation

2D array Position j, k ⇒ edge between nodes nj , nk

Example

4

Adjacency Matrix

Representation (cont.)

Single array for entire graph Undirected graph Only upper / lower triangle matrix needed Since nj, nk implies nk , nj Unweighted graph Matrix elements ⇒ boolean Weighted graph Matrix elements ⇒ weight

7

Adjacency Set / Map

Representation

For each node, store Set or map of neighbors / successors For unweighted graph Use set of neighbors For weighted graph Use map of neighbors, w/ value = weight of edge Undirected graph For every edge (a, b) Nodes a & b need to store each other as neighbor For directed graph May also need to store map of predecessors

8

Graph Space Requirements

Adjacency matrix

½ N^2 entries (for graph with N nodes, E edges) Many empty entries for large, sparse graphs

Adjacency list

2×E entries

Adjacency set / map

2×E entries Space overhead per entry Higher than for adjacency list

9

Graph Time Requirements

Adjacency matrix

Can find individual edge (a,b) quickly Examine entry in array Edge[a,b] Constant time operation

Adjacency list / set / map

Can find all edges for node (a) quickly Iterate through collection of edges for a On average E / N edges per node

10

Graph Time Requirements

Average Complexity of operations

For graph with N nodes, E edges

O(E/N)

O(1)

O(1)

O(1)

Adj Set/Map

Find edge O(1) O(E/N)

Enumerate O(N) O(E/N)

edges for node

Delete edge O(1) O(E/N)

Insert edge O(1) O(E/N)

Operation Adj Matrix Adj List

13

Shortest Path – Dijkstra’s Algorithm

Maintain

Nodes with known shortest path from start ⇒ S Cost of shortest path to node K from start ⇒ C[K] Only for paths through nodes in S Predecessor to K on shortest path ⇒ P[K] Updated whenever new (lower) C[K] discovered Remembers actual path with lowest cost

14

Shortest Path – Intuition for Dijkstra’s

At each step in

the algorithm

Shortest paths are known for nodes in S Store in C[K] length of shortest path to node K (for all paths through nodes in { S } ) Add to { S } next closest node

S

15

Shortest Path – Intuition for Djikstra’s

Update distance to J

after adding node K

Previous shortest path to K already in C[ K ] Possibly shorter path to J by going through node K Compare C[ J ] with C[ K ] + weight of (K,J), update C[ J ] if needed

16

Shortest Path – Dijkstra’s Algorithm

S = ∅

P[ ] = none for all nodes C[start] = 0, C[ ] = ∞ for all other nodes

while ( not all nodes in S )

find node K not in S with smallest C[K] add K to S for each node J not in S adjacent to K if ( C[K] + cost of (K,J) < C[J] ) C[J] = C[K] + cost of (K,J) P[J] = K

Optimal solution computed with greedy algorithm

19

Djikstra’s Shortest Path Example

Update C[K] for all neighbors of 1 not in { S }

S = { 1 }

C[2] = min (∞ , C[1] + (1,2) ) = min (∞ , 0 + 5) = 5

C[3] = min (∞ , C[1] + (1,3) ) = min (∞ , 0 + 8) = 8

5 ∞ none

4 ∞ none

1 0 none

C P

20

Djikstra’s Shortest Path Example

Find node K with smallest C[K] and add to S

S = { 1, 2 }

5 ∞^ none

4 ∞ none

1 0 none

C P

21

Dijkstra’s Shortest Path Example

Update C[K] for all neighbors of 2 not in S

S = { 1, 2 }

C[3] = min (8 , C[2] + (2,3) ) = min (8 , 5 + 1) = 6

C[4] = min (∞ , C[2] + (2,4) ) = min (∞ , 5 + 10) = 15

5 ∞ none

1 0 none

C P

22

Dijkstra’s Shortest Path Example

Find node K with smallest C[K] and add to S

S = { 1, 2, 3 }

5 ∞^ none

1 0 none

C P

25

Dijkstra’s Shortest Path Example

Update C[K] for all neighbors of 4 not in S

S = { 1, 2, 3, 4 }

C[5] = min (∞ , C[4] + (4,5) ) = min (∞ , 9 + 9) = 18

1 0 none

C P

26

Dijkstra’s Shortest Path Example

Find node K with smallest C[K] and add to S

S = { 1, 2, 3, 4, 5 }

1 0 none

C P

27

Dijkstra’s Shortest Path Example

All nodes in S, algorithm is finished

S = { 1, 2, 3, 4, 5 }

1 0 none

C P

28

Dijkstra’s Shortest Path Example

Find shortest path from start to K

Start at K Trace back predecessors in P[ ]

Example paths (in reverse)

1 0 none

C P