








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 14
This page cannot be seen from the preview
Don't miss anything!









1
2
Adjacency matrix 2D array of neighbors Adjacency list List of neighbors Adjacency set / map Set / map of neighbors
Affects efficiency / storage
3
Adjacency Matrix
2D array Position j, k ⇒ edge between nodes nj , nk
4
Adjacency Matrix
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
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
½ N^2 entries (for graph with N nodes, E edges) Many empty entries for large, sparse graphs
2×E entries
2×E entries Space overhead per entry Higher than for adjacency list
9
Graph Time Requirements
Can find individual edge (a,b) quickly Examine entry in array Edge[a,b] Constant time operation
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
For graph with N nodes, E edges
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
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
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
15
Shortest Path – Intuition for Djikstra’s
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
P[ ] = none for all nodes C[start] = 0, C[ ] = ∞ for all other nodes
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
19
Djikstra’s Shortest Path Example
5 ∞ none
4 ∞ none
1 0 none
20
Djikstra’s Shortest Path Example
5 ∞^ none
4 ∞ none
1 0 none
21
Dijkstra’s Shortest Path Example
5 ∞ none
1 0 none
22
Dijkstra’s Shortest Path Example
5 ∞^ none
1 0 none
25
Dijkstra’s Shortest Path Example
1 0 none
26
Dijkstra’s Shortest Path Example
1 0 none
27
Dijkstra’s Shortest Path Example
1 0 none
28
Dijkstra’s Shortest Path Example
Start at K Trace back predecessors in P[ ]
1 0 none