0% found this document useful (0 votes)
16 views49 pages

Lecture16 Handouts Proto

The document provides an introduction to graphs as an abstract data type (ADT) that stores entities and their relationships, detailing graph representations such as adjacency matrices and lists. It covers graph traversals, including depth-first and breadth-first methods, and discusses the efficiency of different representations based on the density of edges. Additionally, it highlights the use of Dijkstra's algorithm and the differences between trees and graphs.

Uploaded by

Abbas Sarfraz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views49 pages

Lecture16 Handouts Proto

The document provides an introduction to graphs as an abstract data type (ADT) that stores entities and their relationships, detailing graph representations such as adjacency matrices and lists. It covers graph traversals, including depth-first and breadth-first methods, and discusses the efficiency of different representations based on the density of edges. Additionally, it highlights the use of Dijkstra's algorithm and the differences between trees and graphs.

Uploaded by

Abbas Sarfraz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

1

Lecture #16
• Intro to Graphs
• Graph Traversals
– Depth-first
– Breadth-first
• Dijkstra’s Algorithm

Final Exam: THIS Saturday!


(The Saturday *before* finals week!)
Graphs
2
Graphs
What’s the big picture?
A graph is an ADT that stores a set of entities
and also tracks the relationships between them.

Entities can be anything – people, airports, street


intersections, bank accounts, etc.
Relationships can be bidirectional (friends) or Uses:
unidirectional (one-way flight from LA to SF) and Turn-by-turn
have attributes, e.g., the travel cost from LA to SF. navigation, TikTok
friend network, self-
Graphs can be implemented in many ways (linked driving cars, airline
flight routing, routing
lists, arrays, hash tables, etc.) and there are
network packets on
100s of algorithms to process them. the Internet, etc.
Introduction to Graphs
4

A graph is an ADT that stores a set of entities and also


keeps track of the relationships between all of them.

Examples of Entities Examples of Relationships


People Joe is friends with Linda
Cities LA is 3000 miles from NYC
Web pages ucla.edu links to awesome.com
Introduction to Graphs
5

Each graph holds two types of items:

Vertices (aka Nodes):


A vertex might represent a person, a city or a web page.

Edges (aka Arcs):


An edge simply connects two* vertices to each other.

* Technically, an edge could connect a vertex to itself!


Directed vs. Undirected Graphs
6

There are two major types of graphs…

Directed Graph Undirected Graph


In a directed graph, an edge In an undirected graph, all edges
goes from one vertex to are bi-directional. You can go
another in a specific direction. either way along any edge.

LA NYC Vickie Ben

For example, above we have an


edge that goes from the LA For example, Vickie and Ben are
vertex to NYC vertex, but not mutual contacts on Hinge.
the other way around.

(e.g., there may be a flight from LA to (It would be kinda creepy if Vickie
NYC but not the other way around) liked Ben, but not visa-versa)
7

Representing a Graph in Your Programs

The easiest way to represent a graph is with a


double-dimensional array.

The size of both dimensions of the array is equal to


the number of vertices in the graph.

bool graph[5][5];

Each element in the array indicates whether or not


there is an edge between vertex i and vertex j.
8

Representing a Graph in Your Programs


Each element in the array indicates whether or not
there is an edge between vertex i and vertex j.

bool graph[5][5]; 0
// edge from vertex 0 to vertex 3 4 1
graph[0][3] = true;
graph[1][2] = true; 3 2
graph[3][0] = true;
0 1 2 3 4
0 T As you can see, when we set array[i][j]
1 T to true, it represents a directed edge
from vertex i to vertex j.
2
3 T This is called an adjacency matrix.
4
9

Representing a Graph in Your Programs


Given the provided adjacency matrix, what
does its directed graph look like?

Nodes 0 1 2 3
0 True False True False
1 True False False False
2 False False False True
3 True False True False
For every set of connected vertices i and j, simply
set array[i][j] to true and set array[j][i] to true as well!
graph[0][3] = true; graph[1][2] = true;
graph[3][0] = true; graph[2][1] = true;
So the matrix is symmetrical w.r.t the diagonal!
3 True
2 True 2
True 1
1 3
True 0
0 3 2 1 0
adjacency matrix - how do we represent an undirected graph?
We just saw how to represent a directed graph with an
Representing a Graph in Your Programs
10
An Interesting Property of Adjacency Matrices
11

Consider the following graph: And it’s associated A.M.:

Joe Mary
Joe 0 1 0 0
Mary
Tsuen Lily
0 0 1 0
Tsuen
0 0 0 1
Neato effect: If you multiply the matrix Lily 0 1 0 0
by itself something cool happens!

0 1 0 0 0 1 0 0
Joe 0 0 1 0
0 0 1 0 0 0 1 0
X =
Mary
0 0 0 1
0 0 0 1 0 0 0 1 Tsuen
0 1 0 0
0 1 0 0 0 1 0 0
Lily 0 0 1 0
The resulting matrix shows us which vertices are exactly two edges apart.
An Interesting Property of Adjacency Matrices
12

Consider the following graph: And it’s associated A.M.:

Joe Mary
Joe 0 1 0 0
Mary
Tsuen Lily 0 0 1 0
Tsuen
Neato effect: If you multiply the matrix 0 0 0 1
by itself something cool happens! Lily 0 1 0 0

0 1 0 0 0 1 0 0 Joe 0 0 10 01

X =
Mary
0 0 1 0 0 0 1 0 0 0
1 00 01
Tsuen
0 0 0 1 0 0 0 1 0 10 10 0
0 1 0 0 0 1 0 0 Lily 0 0 01 01
And if we multiply our new matrix by the original matrix again,
we’ll get all vertices that are exactly 3 edges apart!
A swer: We can represent a directed graph by using a map that associates each source vertex with a list of its adjacent vertices:
map<int, list<int>> vertex_to_neighbors;
And performing the operation:
vertex_to_neighbors[src].push_back(target);
indicates that there is an edge from vertex src to vertex target.
This is called an adjacency list!
structure(s) might we use to represent the edges in a graph?
Assuming each vertex is identified by a number, what other data
13
Another Way to Represent a Graph
The Adjacency List
14

Let's see how it works!

graph target
0 3 1
map<int, list<int>> graph;
NULL NULL
// edge from node 0 to node 3
src 2 0
graph[0].push_back(3); NULL
graph[2].push_back(0);
3 3
graph[0].push_back(1); NULL
graph[3].push_back(3);
0
So for each entry j, in list i, this 3 1
means that there is an edge from
vertex i to vertex j. 2
The Adjacency List
15

We could also implement an adjacency list


with an array of linked lists!
graph target
[0] 0 3 1
list<int> graph[4];
map<int, list<int>> graph;
[1] nullptr NULL NULL
// edge
worksfrom
the same!
node 0 to node 3
src[2] 2 0
graph[0].push_back(3); NULL
[3] nullptr
graph[2].push_back(0);
[4] 3 3
graph[0].push_back(1); NULL
graph[3].push_back(3);
0
So for each entry j, in list i, this 3 1
means that there is an edge from
vertex i to vertex j. 2
16

Which Representation Should You Use?

When should you use an adjacency matrix vs. an adjacency list?

Scenario #1:
We’ve got 10,000,000 users who have relationships with each other –
typically each person is friends with just a few hundred other people.

What data structure would you choose?

Option A: An adjacency matrix


(a 10 million by 10 million adjacency matrix would be 100 trillion cells)

Option B: An adjacency list


(10 million linked lists, each holding roughly 500 items would be 5 billion items)

Answer: An adjacency list is more efficient by a factor of over 20,000!


17

Which Representation Should You Use?

When should you use an adjacency matrix vs. an adjacency list?

Scenario #2:
We’ve got 1,000 cities, with airlines offering flights from
every city to almost every other city.

What data structure would you choose?

Option A: An adjacency matrix


(a 1000 by 1000 adjacency matrix would be 1 million cells)

Option B: An adjacency list


(1000 linked lists, each holding roughly 1000 items would be 1 million items)

one that's easier to implement.


Answer: This one's a tie... Both data structures are about the same size. Pick the
18

Which Representation Should You Use?

When should you use an adjacency matrix vs. an adjacency list?

Use an adjacency matrix if you Use an adjacency list if you


have lots of edges between have few edges between
vertices but few vertices vertices and lots of vertices
(< 10,000 vertices). (> 10,000 verices).

A graph that has many A graph that has few edges


edges between the vertices between the vertices is
is called a “dense graph”. called a “sparse graph”.

Let’s see examples of both…


Dense(r) Graphs
19

Caltech student
friendships on Facebook
Sparse Graphs
20

High-school dating habits Intra-website links

Street maps
Trees vs. Graphs
21

As you might have guessed, graphs


are like trees in many ways!
(in fact a tree is a type of graph)

What's one major difference


between trees and graphs?

Hint:
8 8

6 2 6 2

3 5 3 5

As we'll see this will make graph traversals a bit more difficult!
Answer: Trees are "acyclic" whereas graphs may have cycles!
Graph Traversals
22

There are two types of graph traversals:


Depth-first and Breadth-first

A Depth-first Traversal keeps moving A Breadth-first Traversal explores


forward until it hits a dead end or a the graph in growing concentric circles,
previously-visited vertex… then it exploring all vertices 1 away from the
backtracks and tries another path start, then 2 away, then 3 away, etc.

v (Dead end)
v
v (Previously visited!) v
start v v start v
v v
v v v v v
(Previously visited!)
v v v
… and so on… v
… and so on… 1 step away away
2 steps v
3 steps away
Depth-first Traversal
23

Our depth-first an adjacency list


traversal takes in... representing the graph
Let’s learn the Depth-first Traversal algorithm first:
void depth_first(map<int, list<int>> &graph, the current vertex that
int cur_vertex, we're processing
set<int>& visited)
{ and (an initially empty) set that
tracks which vertices we've
if (visited.contains(cur_vertex)) return;
already visited.

visited.insert(cur_vertex); If we've visited a vertex already, then we


don't want to visit it again - return!
Otherwise, we remember that we visited
cout << "Process " << cur_vertex << "!" << endl;
the vertex by adding it to our set.
Then we process the current vertex.
list<int>& adjacent_vertices = graph[cur_vertex]; Finally, we iterate
for (int target_vertex : adjacent_vertices) through all of vertices
that are adjacent to
depth_first(graph, target_vertex, visited); the current vertex...
}
And then visit each
target vertex This resembles one of our binary tree
recursively.
traversals! Which one? How is it different?
Answer: A pre-order traversal! It differs in that it uses a visited set to deal with cycles.
Depth-first Traversal Challenge
24

In what order will the vertices be visited in this graph by a


depth first traversal, assuming we start in the Start Room and
always visit rooms in a clockwise order?

Teacher’s
Lounge
Treasure
Room

Goblin Witch Bats


Room Hangout Bathroom
Start
Room

Ghoul Warlock
Study Lounge
Ghost
Galley Carey’s
Corner

Answer: Start, goblin, Witch, Treasure, Teacher's, Bat's, Warlock, Ghoul, Carey's, Ghost
Breadth-first Graph Traversal
25

Idea:
Process all of the vertices that are 1 edge away
from the start vertex,
then process all vertices that are two edges away,
then process all vertices that are three edges away,
etc…

What kind of BST traversal is this


similar to? How does it differ?

to deal with cycles and avoiding looping through the same node over and over.
Answer: It's like a level-order traversal (which uses a queue!), but we also have
Breadth-first Graph Traversal
26 the vertex to start
Our breadth-first an adjacency list our processing from.
traversal takes in... representing the graph.

void breadth_first(map<int, list<int>>& graph, int start_vertex) {


set<int> visited; First, we define a set to track
queue<int> to_visit; Then we define a queue to hold vertices we've already visited...
vertices we want to visit.
Track that we're about to
visited.insert(start_vertex); visit the start vertex.
Keep looping until Then add the start vertex
to_visit.push(start_vertex); we have no more to the queue so we visit it.
while (!to_visit.empty()) { vertices to visit.
int cur_vertex = to_visit.front();
to_visit.pop(); Extract the front vertex from
the queue...

cout << "Process " << cur_vertex << endl; Process the
vertex.

list<int>& adjacent_vertices = graph[cur_vertex];


for (int target_vertex: adjacent_vertices) {
if (!visited.contains(target_vertex)) { Now loop through all
vertices that can be
visited.insert(target_vertex); reached from the
to_visit.push(target_vertex); current vertex.

}
then pre-emtively add it to
} If we haven't visited a
our visited set and to our
vertex yet...
} queue so we visit it.
}
27

Breadth-first Traversal Challenge


In what order will the vertices be visited in this graph
by a breadth-first traversal, assuming we start in the
Start Room and always visit rooms in a clockwise order?

Goblin Witch Bat's


Room Hangout Bathroom
Start
Room

Ghoul Warlock
Study Treasure
Lounge Room
Ghost
Galley
Carey’s
Corner

Answer: Start, Goblin, Ghoul, Ghost, Witch, Carey's, Bat's, Treasure, Warlock
Graphs With Weighted Edges
28

What does it mean for a graph to have weighted edges?

It means that each edge connecting vertex u with


vertex v has a weight or cost associated with it.

WA $900
$300
LA $400 NY
$140
$180 $220 FL
TX

This can be used to model things like travel costs, traffic


conditions, the strength of a connection in a social network,
network bandwidth for routing packets, etc.
29

Implementing Weighted Edges


How could we change our adjacency matrix and
adjacency list to support weighted edges?

In an adjacency list, we can map


In an adjacency matrix, we can to a struct holding the target
replace Booleans with weights! vertex and weight rather than
just the target vertex!
struct Edge {
double adjmatrix[5][5]; int vertex;
double weight;
};
0 1 2 3 4 map<int, list<Edge>> adjlist;
0 2.7 1.5 vertex 3 vertex 1

1 0 weight 1.5 weight 2.7


nullptr

2 4.2 2
vertex 0
weight 4.2
3 9.1 nullptr

4 3
vertex 3
weight 9.1
nullptr
30

Lots Of Interesting Algorithms Too!


Here's a short-list of cool algorithms that operate on weighted graphs:

A* Search:
Finds the shortest path from a source vertex
to a target vertex, e.g., for GPS navigation

Dijkstra's Algorithm/Bellman-Ford Algorithm:


Finds the shortest path from a source vertex to all
other vertices, e.g., routing packets in telecom networks

Prim's Algorithm:
Finds the minimum set of edges that connects
all the vertices with minimum cost, e.g.,
optimizing layout for chip/building wiring

And many others!


31

One Final Graph Problem To Leave You With


Consider a salesperson who must travel to N
different cities to sell their goods.
They only want to visit each city once and want to
minimize their travel distance/cost.

Assuming there are direct flights between every city, how


do you find the shortest route that visits each city once?

It's known as the "traveling salesman problem."


In the general case, solutions are O(N!)!!!!
Answer: This is an open graph problem with no known solution!
That's It For Graphs!
32

Hopefully, you have a flavor for the types of


problems we can represent with graphs...

the different ways to represent graphs,


and when to use each approach...

and the variety of algorithms you


can apply to graphs!

Ok, on to final exam review!


Appendix
33
Graphs With Weighted Edges
34

Definition: The weight of a path from vertex u to vertex


v is the sum of the weights of the edges between the
two vertices.

WA $900 Question: What’s the cost of


$300 traveling from LA to NY to
WA?
LA $400 NY
$140 Question: What’s the
$180 $220 FL shortest path from LA to
TX
WA?
Definition: The shortest path between two vertices is
the path with the lowest total cost of edges between the
two vertices. (The shortest path is a set of vertices)
Finding the Shortest Path
35

Question: How can we find the shortest path between


any two nodes in a graph?
Answer: Dijkstra’s Algorithm (the dorm guy?)

Dijkstra’s Algorithm:

the length of
This algorithm determines the shortest path (i.e.
set of vertices) from a start vertex s to all other
vertices in the graph.
A 10 B So Dijkstra(A) would give
7
2 2 us a value of 6 for A to B,
C D a value of 2 for A to C,
2 6 and 4 for A to D.
36

Dijkstra’s Algorithm
Input: A graph G, and a starting vertex s

s G must not have any


A 10 negative edge values.
B
7
2 2
C D
2 6

Output: An array called Dist of optimal distances from s


to every other node in the graph.

Dist A B C D
0 6 2 4
37

Dijkstra’s Algorithm: Basic Idea


Dijkstra's algorithm splits vertices in two distinct sets: the
set of unsettled vertices and the set of settled vertices.

Unsettled vertex: A vertex v is unsettled if we don’t know


the optimal distance to it from the starting vertex s.

Settled vertex: A vertex v is settled if we have learned


the optimal distance to it from the starting vertex s.

Initially all vertices Start vertex


are unsettled.
A
Unsettled
10 B
Unsettled
The algorithm ends 7
once all vertices are 2 2
in the settled set. C
Unsettled D
Unsettled
2 6
38

Dijkstra on a Graph
Assume that all vertices are
infinitely far away to start… 10
A
A B
Since we start at vertex A, 7
2 2
we know it’s the closest C D
vertex to us! How far is it? 2 6
Zero steps away! We can Distance 0 10 2 7
settle it immediately! (Best known so far)

A B C D
Now let’s see which unsettled vertices
we can reach directly from A.
• B is 10 units away.
• C is 2 units away.
• D is 7 units away.
And
Going going
directly
Ok, let’s directly
from
compare from
A toA
these B
C to
is D
costs only
is our
to only
10
2 units
units
7 units
away,
away,
current away,
which
which
best which
isis in
costs
less
is less
our than
than
table…infinity,
infinity,
so so
I’llI’ll
update
updatethisthis
entry…
entry
entrytoo…
too…
39

Dijkstra on a Graph
Ok, so now we know the costs to
travel to all vertices directly 10
A
A B
reachable from A.
7
2 2
Which unsettled vertex is closest C D
2 6
to A?
Distance 0 10 2 7
Right! C is closest to A. (Best known so far)

A B C D
If we go directly to C (A → C), it costs us 2 units. Is
there any possible way I can travel to C cheaper by going
through B or through D first?
No
AndIway!
So know
if Ifthat
I travel
I travel through
if I travel
through B (e.g.
directly
D (e.g. A
A →from
D→A B
…→ →…
to C, IC),
at
C),→ I
aknow
cost
know it’llme
it’ll2cost
of units,costatme
that’s at least
least
the 10(plus
7fastest
units units (plusroute.
possible
whateverwhatever
it costsit to
Thereforecosts
to travel
travel
we through
canthrough
settle atthe
Cthe other
2other
units. vertices
vertices to to
C). C).
40

Dijkstra on a Graph
At this point, we know the
shortest path from A to C. Now A
A 10 B
let’s see if we can travel 7
through C to reach one of our 2 2
other unsettled vertices faster. C D
2 6
Ok, which unsettled vertices can
be reached directly from C?
Distance 0 10
8 2 74
(Best known so far)
• B is 6 units away. A B C D
• D is 2 units away.

Let’s
Let’sdodoBDfirst.
next.WeWeknow
knowwewecan
canget
getfrom
fromAAtotoCCinin22
units, and we can directly go from C to B in 6 units, so we
units, and we can directly go from C to D in 2 units, so
can reach B in just 8 units!
we can reach D in just 4 units!
IsIsour
ournew
newdistance
distancetotoBDbetter
betterthan
thanour
ourold
oldone?
one?
You bet!! Let’s update
ourour table!
Yup!! Let’s update table again!
41

Dijkstra on a Graph
Ok, so now we know the best
cost to get to all unsettled 10
A
A B
vertices, assuming we travel
7
through C. 2 2
C D
Which unsettled vertex is closest 2 6
to A now?
Distance 0 10 8 2 4
74
Right! D is closest.
(Best known so far)

A B C D
If we take the path A → C → D, it costs us 4 units. Is
there any possible way I can travel to D cheaper by going
through B first?
No
So Iway!
knowIfthat
I travel through
if I travel B (e.g.
from A →AC→ →CD, B a→cost
→at … →ofD),
4
I knowthat’s
units, it’ll cost
the me at least
fastest 8 units.
possible That’s
route. much longer!
Therefore we can
settle D at 4 units.
42

Dijkstra on a Graph
At this point, we know the
shortest path from A to D. Now A
A 10 B
let’s see if we can travel 7
through D to reach one of our 2 2
other unsettled vertices faster. C D
2 6
Ok, which unsettled vertices can
be reached directly from D?
Distance 0 10
8 2 4
6 74
(Best known so far)
• B is 2 units away. A B C D

Let’s check B. We know we can get from A to D in 4


units, and we can directly go from D to B in 2 units, so
we can reach B in just 6 units!
Is our new distance to B better than our old one?
You bet!! Let’s update our table!
43

Dijkstra on a Graph
Ok, so now we know the best
cost to get to all unsettled 10
A
A B
vertices, assuming we travel
7
through D. 2 2
C D
Which unsettled vertex is closest 2 6
to A now?
Distance 0 10 6 2 4
74
Right! B is closest.
(Best known so far)

A B C D
If we take the path A → C → D → B it costs us 6 units.
Is there any possible way I can travel to B cheaper?
And now that all of our vertices are settled, we are
Noguaranteed
way! Theretoare
have
no found
other vertices
the minimum
we can
travel
go through
distances
that will make our to
path
each
from
of A
our
tovertices!
B shorter.
Therefore we can settle B at 6 units.
Dijkstra
44

And now I’ll give you the more formal algorithm…

Born: 11 May 1930, Rotterdam, Netherlands


Died: 6 August 2002, Nuenen, Netherlands
45

Dijkstra’s Algorithm
Dijkstra’s Algorithm uses 2 data structures:

1. An array called Dist that holds the the current best known
cost to get from s to every other vertex in the graph.
For each vertex i, Dist[i] starts out with a value of:
• 0 for vertex s
• Infinity for all other vertices

Dist from
G A B C D
s vertex s
10 to… 0
A B
2 7 Idea: We start at node A so we’re
2
C D 0 steps away from node A. We
2 assume the other vertices are
infinitely far away from A.
46

Dijkstra’s Algorithm
Dijkstra’s Algorithm uses 2 data structures:

2. An array called Done that holds true for each vertex


that has been fully processed, and false otherwise.
For each vertex i, Done[i] starts out with a value of false.

Dist from
G A B C D
s vertex s
10 to… 0
A B
2 7 Done A B C D
2
C D false false false false
2
47

Dijkstra’s Algorithm
u v
While there are still unprocessed vertices: s 10
A B
Set u = the closest unprocessed vertex to 7
2
the start vertex s C D 2
Mark vertex u as processed: Done[u] = true. v 2
We now know how to reach u optimally from s Previous cost:
Loop through all unprocessed vertices: New cost:
New cost: 0
0 ++ 10
2 ==210
Set v = the next unprocessed vertex
Dist
If there’s an edge from u to v then compare:
from
a. the previously computed path from s to vertex A B C D
v (i.e. Dist[v]) OR s to… 0 10 2
b. the path from s to u, and then from u
to v (I.e. Dist[u] + weight(u,v)) Done A B C D
true false false false
false
If the new cost is less than old cost then
u v v
Set Dist[v] = Dist[u] + weight(u,v)
48

Dijkstra’s Algorithm
u v
While there are still unprocessed vertices: s 10
A B
Set u = the closest unprocessed vertex to 7
2
the start vertex s C D 2
Mark vertex u as processed: Done[u] = true. u2 vv
We now know how to reach u optimally from s Previous cost: 7
Loop through all unprocessed vertices: New cost: 0
2+7 2=74
Set v = the next unprocessed vertex
Dist
If there’s an edge from u to v then compare:
from
a. the previously computed path from s to vertex A B C D
v (i.e. Dist[v]) OR s to… 0 10 2 4
7
b. the path from s to u, and then from u
to v (I.e. Dist[u] + weight(u,v)) Done A B C D
true false
true false false
false
If the new cost is less than old cost then
u v u vv
Set Dist[v] = Dist[u] + weight(u,v)
49

Dijkstra’s Algorithm
s uv
While there are still unprocessed vertices: 10 B
A
Set u = the closest unprocessed vertex to 7
the start vertex s 2 2
C D
Mark vertex u as processed: Done[u] = true. 2 u
We now know how to reach u optimally from s Previous cost: 10
Loop through all unprocessed vertices: New cost: 4 + 2 = 6
Set v = the next unprocessed vertex
Dist
If there’s an edge from u to v then compare:
from
a. the previously computed path from s to vertex A B C D
v (i.e. Dist[v]) OR s to… 0 6 2
10 4
b. the path from s to u, and then from u
to v (I.e. Dist[u] + weight(u,v)) Done A B C D
true false
true false
true false
false true
If the new cost is less than old cost then
Set Dist[v] = Dist[u] + weight(u,v)
uv u
And we’re done! The Dist array contains the results.

You might also like