Graph Implementations & Shortest Path: Matrix, List, Dijkstra - Prof. Nelson Padua-Perez, Study notes of Computer Science

An in-depth analysis of various graph implementations, including adjacency matrix, adjacency list, and adjacency set/map. It also covers the single source shortest path algorithm, specifically dijkstra's algorithm, which is used to find the shortest path between nodes in a graph.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-xvz
koofers-user-xvz 🇺🇸

10 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Graph Implementations &
Single Source Shortest Path
Algorithm
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Graph Implementations & Shortest Path: Matrix, List, Dijkstra - Prof. Nelson Padua-Perez and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Graph Implementations &Single Source Shortest Path

Algorithm

Department of Computer ScienceUniversity of Maryland, College Park

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

Adjacency Matrix

Representation (cont.)

Single array for entire graph Undirected graph

Only upper / lower triangle matrix needed Since n

, nj

implies nk

, nk

j

Unweighted graph

Matrix elements

boolean

Weighted graph

Matrix elements

weight

Adjacency List

Representation

For each node, store

List of neighbors / successors

Linked list Array list For weighted graph

Also store weight for each edge Undirected graph

For every edge (a, b)

Nodes a & b need to store each other as neighbor Directed graph

May also need to store list of predecessors

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

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

Graph Time Requirements

Average Complexity of operations

For graph with N nodes, E edges

O(1)O(1)O(1) O(E/N)

Adj Set/Map

O(E/N)

O(1)

Find edge

O(E/N)

O(N)

Enumerateedges for node

O(E/N)

O(1)

Delete edge

O(E/N)

O(1)

Insert edge

Adj List

Adj Matrix

Operation

Choosing Graph Implementations Graph density

Ratio edges to nodes (dense vs. sparse)

Graph algorithm

Neighbor based

For each node X in graph

For each neighbor Y of X

// adj list faster if sparse

doWork( )

Connection based

For each node X in …

For each node Y in …if (X,Y) is an edge

// adj matrix faster if dense

doWork( )

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

Shortest Path – Intuition for Dijkstra’s^ At each step inthe algorithm

Shortest pathsare known fornodes in

S

Store in

C[K]

length ofshortest path tonode K (for allpaths throughnodes in { S } ) Add to { S } nextclosest node

S

Shortest Path – Dijkstra’s Algorithm S =^ ∅∅∅∅ P[ ] = none for all nodesC[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 Sfor 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

Dijkstra’s Shortest Path Example Initial state S =^

none ∞∞∞∞ 5

none ∞∞∞∞ 4

none ∞∞∞∞ 3

none ∞∞∞∞ 2

none 0 1

P

C

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 (

∞∞∞∞^

C[3] = min (

∞∞∞∞^

, C[1] + (1,3) ) = min (

∞∞∞∞^

none ∞∞∞∞ 5

none ∞∞∞∞ 4

1 8 3

1 5 2

none 0 1

P

C

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to S S = { 1, 2 }

none ∞∞∞∞ 5

none ∞∞∞∞ 4

1 8 3

1 5 2

none 0 1

P

C