0% found this document useful (0 votes)
18 views

Lab Graph

The document discusses different graph representations including adjacency matrix, adjacency list, and incident matrix. It provides examples of each and traces of Dijkstra's and Bellman-Ford algorithms to find shortest paths in graphs. Pseudocode for the Dijkstra and Bellman-Ford algorithms is also given.

Uploaded by

Aini Ayunni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab Graph

The document discusses different graph representations including adjacency matrix, adjacency list, and incident matrix. It provides examples of each and traces of Dijkstra's and Bellman-Ford algorithms to find shortest paths in graphs. Pseudocode for the Dijkstra and Bellman-Ford algorithms is also given.

Uploaded by

Aini Ayunni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Graph

Adjacency matrix, adjacency lists, incident matrix:


Example A:
Incident matrix (format):

WEIGHT

HEAD 0 OR 1

Adjacency matrix Adjacency list Incident matrix


Head List w1 w2 w3 w4 w5
1 2 3 4 5 1 3, 5 1
1 0 0 1 0 1 2 4, 5 2
2 0 0 0 1 1 3 1 3
3 1 0 0 0 0 4 3 4
4 0 0 1 0 0 5 1, 4 5
5 1 0 0 1 0
Example B:

Adjacency matrix Adjacency list Incident matrix


A B C D E F G H Head List w1 w2 w3 w4 w5
A 0 1 1 1 0 0 0 0 A B, C, D A
B 1 0 0 0 1 1 0 0 B A, E, F B
C 1 0 0 0 0 0 1 0 C A, G C
D 1 0 0 0 0 0 0 1 D A, H D
E 0 1 0 0 0 0 0 1 E B, H E
F 0 1 0 0 0 0 0 1 F
Dijkstra’s algorithm trace to find the shortest path from vertex a to vertex e
directed graph (don’t accept -ve value):

init 1 2 3 4 5
active a b c f d e
vertex
a 0
b ∞ 7
c ∞ 9 9
d ∞ ∞ 22 20 20
e ∞ ∞ ∞ ∞ ∞ 26
f ∞ 14 14 11

Bellman-Ford’s algorithm trace to find the shortest path from its start vertex to its
end vertex directed graph (accept -ve value):

Edges: source A, source B, AC, BA, DB, CD

Iteration init 1 2 3
Source 0
A ∞ 41
B ∞ 6
C ∞ 7 4
D ∞ 7 6
Dijkstra Algorithm’s pseudocode to find the shortest path:

DijkstraAlgorithm (weighted simple digraph, vertex first)


for all vertices x
currDist(x) = infinity;
currDist(first) = 0;
toBeChecked = all vertices;
while toBeChecked is not empty
x = a vertex in toBeChecked with minimal currDist(x);
remove x from toBeChecked;
for all vertices x adjacent to y
if currDist(x) > currDist(y) + weight(edge(yx))
currDist(x) = currDist(y) + weight(edge(yx));
predecessor(x) = y;

Bellman Ford Algorithm’s pseudocode to find the shortest path:

FordAlgorithm(weighted simple digraph, vertex first)


for all vertices x
currDist(x) = infinity;
currDist(first) = 0;
while there is an edge(yx) such that currDist(x) > currDist(y) +
weight(edge(yx));
currDist(x) = currDist(y) + weight(edge(yx));

You might also like