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

Graphs

The document provides an overview of graph terminology, types of graphs, and their applications, including undirected, directed, and weighted graphs. It covers key concepts such as graph traversals, shortest path algorithms like Dijkstra's, and various implementations of the Graph Abstract Data Type (ADT). Additionally, it discusses the importance of graphs in solving real-world problems across different fields such as transportation and computer networks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Graphs

The document provides an overview of graph terminology, types of graphs, and their applications, including undirected, directed, and weighted graphs. It covers key concepts such as graph traversals, shortest path algorithms like Dijkstra's, and various implementations of the Graph Abstract Data Type (ADT). Additionally, it discusses the importance of graphs in solving real-world problems across different fields such as transportation and computer networks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

Graphs

Learning Outcomes

 Explain graph terminology and the different types of


graphs

 Examine Graph ADT and different implementations


of the Graph ADT

 Apply breadth-first and depth-first search traversal


algorithms

 Demonstrate the use of graphs in solving problems

2
Lecture Scope

 Graph terminology

 Applications

 Undirected graph

 Directed graph

 Weighted graph

 Graph traversals

 Shortest path

 Dijkstra algorithm

 Graph representation 3
Graphs - Terminology
 Like trees, graphs are made up of nodes and the connections between
those nodes

 In graph terminology, we refer to the nodes as vertices and refer to the


connections among them as edges

 Vertices are typically referenced by a name or label

 Edges are referenced by a pairing of the vertices (A, B) that they connect

Undirected Directed (Digraph) Weighted

4
Graphs - Terminology (cont…)

 Two vertices are said to be adjacent if there is an edge connecting


them

 Adjacent vertices are sometimes referred to as neighbors

 An edge of a graph that connects a vertex to itself is called a self-loop


or sling
 For example, {v1, v1}

 A path is a sequence of edges that connects two vertices in a graph

 The length of a path in is the number of edges in the path (or the
number of vertices minus 1)

5
Graphs - Terminology (cont…)

 A sub-graph of a graph a subset of the vertices and a subset of the


edges that connected the subset of vertices in the original graph

6
Graphs - Terminology (cont…)

 A cycle is a path in which the first and last vertices


are the same and none of the edges are repeated

 A graph that has no cycles is called acyclic

 A graph is connected if there exists a path between


any two vertices

7
Graphs - Applications
 Electronic circuits
 Printed circuit board

 Integrated circuit

 Transportation networks
 Highway network

 Flight network

 Computer networks
 Local area network cslab1a cslab1b

math.brown.edu

 Internet
cs.brown.edu

 Web brown.edu

qwest.net
att.net
 Databases
cox.net
 Entity-relationship diagram John
8
Paul
David
Graphs - Applications (cont…)

Some Example Applications of Graph:

 Finding the least congested route between two


phones, given connections between switching
stations.

 Determining if there is a way to get from one page


to another, just by following links.

 Finding the shortest path from one city to another.

9
Undirected Graphs
 An undirected graph is a graph where the pairings representing the edges
are unordered

 We will define an Undirected Graph ADT as a collection of vertices

V = {v1, v2, ..., vn}


 The number of vertices is denoted by

|V| = n
 Associated with this is a collection E of unordered pairs {vi, vj} termed edges
which connect the vertices

 An undirected graph:

10
Undirected Graphs (cont…)

 An edge in an undirected graph can be traversed in either direction

 An undirected graph is considered complete if it has the maximum


number of edges connecting vertices

 There are a number of data structures that can be used to


implement abstract undirected graphs
 Adjacency matrices

 Adjacency lists

11
Undirected Graphs (cont…)

 Consider this collection of vertices

V = {v1, v2, ..., v9}


where |V| = n = 9

12
Undirected Graphs (cont…)

 Associated with these vertices are |E| = 5 edges

E = {{v1, v2}, {v3, v5}, {v4, v8}, {v4, v9}, {v6, v9}}
 The pair {vj , vk} indicates that both vertex vj is adjacent to vertex vk
and vertex vk is adjacent to vertex vj

13
Undirected Graphs (cont…)

 Example: given the |V| = 7 vertices


V = {A, B, C, D, E, F, G}

 and the |E| = 9 edges


E = {{A, B}, {A, D}, {A, E}, {B, C}, {B, D}, {B, E}, {C, E}, {C, F}, {D, E}}

14
Undirected Graphs - Path (cont…)

 A path in an undirected graph is an ordered sequence


of vertices

(v0, v1, v2, ..., vk)

where {vj – 1, vj} is an edge for j = 1, ..., k


 Termed a path from v0 to vk
 The length of this path is k

15
Undirected Graphs - Path (cont…)

 A path of length 4:
(A, B, E, C, F)

 A path of length 5:
(A, B, E, C, B, D)

 A trivial path of length 0:


(A)

16
Undirected Graphs - Degree

 The degree of a vertex is defined as the number of adjacent vertices


degree(A) = degree(D) = degree(C) = 3

degree(B) = degree(E) = 4

degree(F) = 1
degree(G) = 0

 Those vertices adjacent to a given vertex are its neighbors


17
Undirected Graphs - Connectedness

 An undirected graph is considered connected if for any two vertices in the


graph there is a path between them

 Two vertices vi, vj are said to be connected if there exists a path from vi to vj

A connected graph An unconnected graph


18
Undirected Graphs - Connectedness (cont…)

 An example of an undirected graph that is not


connected:

19
Directed Graphs

 A directed graph, sometimes referred to as a digraph,


is a graph where the edges are ordered pairs of
vertices

 This means that the edges (A, B) and (B, A) are


separate, directional edges in a directed graph

20
Directed Graphs (cont…)

 In a directed graph, the edges on a graph are be


associated with a direction
 Edges are ordered pairs (vj, vk) denoting a connection from
vj to vk
 The edge (vj, vk) is different from the edge (vk, vj)

 Streets are directed graphs:


 In most cases, you can go two ways unless it is a one-way
street
21
Directed Graphs (cont…)

 A directed graph with


 vertices A, B, C, D

 edges (A, B), (A, D), (B, C), (B, D) and (D, C)

22
Directed Graphs (cont…)

 Given our graph of nine vertices V = {v1, v2, …v9}


 These six pairs (vj , vk) are directed edges

E = {(v1, v2), (v3, v5), (v5, v3), (v6, v9), (v8, v4), (v9, v4)}

23
Directed Graphs - Paths

 a path in a direct graph is a sequence of directed edges that connects


two vertices in a graph

 A path in a directed graph is an ordered sequence of vertices

(v0, v1, v2, ..., vk)

where (vj – 1, vj) is an edge for j = 1, ..., k

 A path of length 5 in this graph is


(v1, v4, v5, v3, v5, v2)

 A simple cycle of length 3 is


(v8, v4, v5, v8)

24
Directed Graphs - In and Out Degrees

 The degree of a vertex must be modified to consider both cases:


 The out-degree of a vertex is the number of vertices which are
adjacent to the given vertex

 The in-degree of a vertex is the number of vertices which this


vertex is adjacent to

 In this graph:
in_degree(v1) = 0 out_degree(v1) = 2

in_degree(v5) = 2 out_degree(v5) = 3

25
Directed Graphs - Sources and Sinks

 Some definitions:

 Vertices with an in-degree of zero are described as sources

 Vertices with an out-degree of zero are described as sinks

 In this graph:

 Sources: v1, v6, v7

 Sinks: v2, v9

26
Directed Graphs - Connectedness

 A connected directed graph and an unconnected


directed graph:

27
Directed Graphs - Connectedness (cont…)
Strongly Connected
 Two vertices vj, vk are said to be connected if there
exists a path from vj to vk
 A graph is strongly connected if there exists a directed
path between any two vertices
Weakly Connected

 A graph is weakly connected there exists a path between


any two vertices that ignores the direction

 In this graph:
 The sub-graph {v3, v4, v5, v8} is strongly
connected

 The sub-graph {v1, v2, v3, v4, v5, v8} is


weakly connected
28
Directed Acyclic Graphs
 A directed acyclic graph is a directed graph which has no cycles
 These are commonly referred to as DAGs

 They are graphical representations of partial orders on a finite number of


elements

These two are DAGs:

This directed graph is not acyclic: Directed Cycle

29
Weighted Graphs

 A weighted graph, sometimes called a network, is a graph with weights (or


costs) associated with each edge

 The weight of a path in a weighted graph is the sum of the weights of the
edges in the path

 Weighted graphs may be either undirected or directed

 For weighted graphs, we represent each edge with a triple including the
starting vertex, ending vertex, and the weight (Boston, New York, 120)

30
Weighted Graphs (cont…)

 A weight may be associated with each edge in a graph


 This could represent distance, energy consumption, cost, etc.

 Such a graph is called a weighted graph

 Pictorially, we will represent weights by numbers next to the edges

31
Weighted Graphs (cont…)

 In a weighted directed graphs, each edge is associated with a value

 Unlike weighted undirected graphs, if both (vj, vk) and (vj, vk) are
edges, it is not required that they have the same weight

6.4

7.5 7.3 6.8


6.7
5.9
4.1
3.2
4.7
5.4 4.5

32
Weighted Graphs (cont…)

 We could use an undirected network to represent


flights between cities

 The weights are the cost

33
Weighted Graphs (cont…)

 A directed version of the graph could show different


costs depending on the direction

34
Weighted Graphs (cont…)

 The length of a path within a weighted graph is the sum of all of the
edges which make up the path
 The length of the path (A, D, G) in the following graph is 5.1 + 3.7 = 8.8

35
Weighted Graphs (cont…)

 Different paths may have different weights

 Another path is (A, C, F, G) with length 1.2 + 1.4 + 4.5 = 7.1

36
Weighted Graphs (cont…)

 Problem: find the shortest path between two vertices


 Here, the shortest path from A to H is (A, C, F, D, E, G) with length 5.7

37
Implementing Graphs

 Strategies for implementing graphs:

 Adjacency lists
 use a set of graph nodes which contain a linked list storing the edges within
each node

 for weighted graphs, each edge would be stored as a triple including the
weight

 Adjacency matrices
 use a two dimensional array

 each position of the array represents an intersection between two vertices in


the graph

 each intersection is represented by a boolean value indicating whether or not


the two vertices are connected
38
Implementing Graphs (cont…)

Graph
ADT (general
concept)

Adjacency Adjacency list Data structure


matrix (Array) (specific)
(Linked list)

39
Graph Representation
a b c a b c d e f
a 0 0 0 0 1 0

adjacency matrix
d e f b 0 0 0 1 0 1
c 0 0 0 0 0 0
a e d 0 1 0 0 1 1
e 1 0 0 1 0 0
b d f
f 0 1 0 1 0 0

c
ae bd bf de df
a 1 0 0 0 0
d b e f

incidence matrix
b 0 1 1 0 0

e a d c 0 0 0 0 0
d 0 1 0 1 1
f b d e 1 0 0 1 0
f 0 0 1 0 1 40
adjacency list
Adjacency Matrix Representation

 Adjacency Matrix uses a 2-D array of dimension |V|x|V| for edges.


(For vertices, a 1-D array is used)

 The presence or absence of an edge, (v, w) is indicated by the entry


in row v, column w of the matrix.

 For an unweighted graph, boolean values could be used.

 For a weighted graph, the actual weights are used.

41
Adjacency Matrix Representation (cont…)

Notes on Adjacency Matrix

 For undirected graph, the adjacency matrix is always symmetric.

 In a Simple Graph, all diagonal elements are zero (i.e. no edge


from a vertex to itself).

42
Adjacency Matrix Representation (cont…)

 An undirected graph and it's adjacency matrix:

 A directed graph and its adjacency matrix:

43
Adjacency Matrix Representation (cont…)

 Requiring more memory but also faster, an adjacency matrix


 The matrix entry (j, k) is set to true if there is an edge (vj, vk)

1 2 3 4 5 6 7 8 9
1 T T
2
3 T
4 T T
5 T T T
6 T
7 T
8 T
9

44
Adjacency List Representation
 This involves representing the set of vertices adjacent to each vertex as
a list. Thus, generating a set of lists.

 This can be implemented in different ways.

 In the following representation:


 Vertices as a one dimensional array

 Edges as an array of linked list (the emanating edges of vertex 1 will be in the
list of the first element, and so on, …

vertices edges

1  1,2  1,3  Null

2  2,3  2,4  Null

3  Empty

4  4,1  4,2  4,3  Null


45
Adjacency List Representation (cont…)

 Most efficient for algorithms is an adjacency list


 Each vertex is associated with a list of its neighbors

1 •→2→4
2 •
3 •→5
4 •→2→5
5 •→2→3→8
6 •→9
7 •→9
8 •→4
9 •

46
Common Graph Algorithms

 There are a number of a common algorithms that may


apply to undirected, directed, and/or weighted graphs

 These include
 various traversal algorithms

 algorithms for finding the shortest path

 algorithms for finding the least costly path in a network

 algorithms for answering simple questions (such as connectivity)

47
Graph Traversals

 Traversals of graphs are also called searches

 There are two main types of graph traversal algorithms


 breadth-first: behaves much like a level-order traversal of a tree
 Breadth-first requires a queue

 depth-first: behaves much like the preorder traversal of a tree


 Depth-first requires a stack

 One difference: there is no root node present in a graph

 Graph traversals may start at any vertex in the graph

48
Breadth-First Traversal

 In this method, after visiting a vertex v, we must visit all its adjacent
vertices w1, w2, w3, ..., before going down next level to visit vertices
adjacent to w1 etc.

 The method can be implemented using a queue.

 A boolean array is used to ensure that a vertex is enqueued only


once.

 Note: Adjacent vertices can be enqueued in any order; but to obtain


a unique traversal, we will enqueue them in alphabetical order.

49
Breadth-First Traversal (cont…)

 We can construct a breadth-first traversal for a graph using a queue


and an iterator

 We will use the queue to manage the traversal and the iterator to
build the result

 Breadth-first traversal algorithm overview

 enqueue the starting vertex, and mark it as “visited”

 loop until queue is empty

 dequeue first vertex and add it to iterator

 enqueue each unmarked vertex adjacent to the dequeued vertex

 mark each vertex enqueued as “visited”


50
Breadth-First Traversal (cont…)

 Consider implementing a breadth-first traversal on a graph:


 Choose any vertex, mark it as visited and push it onto queue

 While the queue is not empty:


 Pop to top vertex v from the queue

 For each vertex adjacent to v that has not been visited:


 Mark it visited, and

 Push it onto the queue

 This continues until the queue is empty


 Note: if there are no unvisited vertices, the graph is connected,

 The size of the queue is O(|V|)

51
Breadth-First Traversal (cont…)

iteration v queue
init a
1 a b c f
a(0)
(1) b (2)
(0) c (3)
(0)
2 b c f e g
3 c f e g
d(7)
(0) e (5)
(0) f (4)
(0) 4 f e g
5 e g d
6 g d h
g(6)
(0) h (8)
(0)
7 d h
8 h

52
Breadth-First Traversal (cont…)
/**
* Returns an iterator that performs a breadth first
* traversal starting at the given index.
*
* @param startIndex the index from which to begin the traversal
* @return an iterator that performs a breadth first traversal
*/
public Iterator<T> iteratorBFS(int startIndex)
{
Integer x;
QueueADT<Integer> traversalQueue = new LinkedQueue<Integer>();
UnorderedListADT<T> resultList = new ArrayUnorderedList<T>();

if (!indexIsValid(startIndex))
return resultList.iterator();

boolean[] visited = new boolean[numVertices];


for (int i = 0; i < numVertices; i++)
visited[i] = false;

traversalQueue.enqueue(new Integer(startIndex));
visited[startIndex] = true;

53
Breadth-First Traversal (cont…)
while (!traversalQueue.isEmpty())
{
x = traversalQueue.dequeue();
resultList.addToRear(vertices[x.intValue()]);

// Find all vertices adjacent to x that have not been visited


// and queue them up

for (int i = 0; i < numVertices; i++)


{
if (adjMatrix[x.intValue()][i] && !visited[i])
{
traversalQueue.enqueue(new Integer(i));
visited[i] = true;
}
}
}
return new GraphIterator(resultList.iterator());
}

54
Depth-First Traversal

 In this method, After visiting a vertex v, which is adjacent to


w1, w2, w3, ...; Next we visit one of v's adjacent vertices, w1
say. Next, we visit all vertices adjacent to w1 before coming
back to w2, etc.

 Must keep track of vertices already visited to avoid cycles.

 The method can be implemented using a stack.

 The method can be implemented using recursion or iteration.

 Note: Adjacent vertices can be pushed in any order; but to


obtain a unique traversal, we will push them in reverse
alphabetical order.
55
Depth-First Traversal (cont…)

 Consider implementing a depth-first traversal on a graph:


 Choose any vertex, mark it as visited

 From that vertex:


 If there is another adjacent vertex not yet visited, go to it

 Otherwise, go back to the most previous vertex that has not yet had all
of its adjacent vertices visited and continue from there

 Continue until no visited vertices have unvisited adjacent vertices

 Two implementations:
 Recursive

 Iterative
56
Depth-First Traversal (cont…)
/**
* Returns an iterator that performs a depth first traversal
* starting at the given index.
*
* @param startIndex the index from which to begin the traversal
* @return an iterator that performs a depth first traversal
*/
public Iterator<T> iteratorDFS(int startIndex)
{
Integer x;
boolean found;
StackADT<Integer> traversalStack = new LinkedStack<Integer>();
UnorderedListADT<T> resultList = new ArrayUnorderedList<T>();
boolean[] visited = new boolean[numVertices];

if (!indexIsValid(startIndex))
return resultList.iterator();

for (int i = 0; i < numVertices; i++)


visited[i] = false;

traversalStack.push(new Integer(startIndex));
resultList.addToRear(vertices[startIndex]);
visited[startIndex] = true; 57
Depth-First Traversal (cont…)
while (!traversalStack.isEmpty())
{
x = traversalStack.peek();
found = false;

// Find a vertex adjacent to x that has not been visited


// and push it on the stack
for (int i = 0; (i < numVertices) && !found; i++)
{
if (adjMatrix[x.intValue()][i] && !visited[i])
{
traversalStack.push(new Integer(i));
resultList.addToRear(vertices[i]);
visited[i] = true;
found = true;
}
}
if (!found && !traversalStack.isEmpty())
traversalStack.pop();
}
return new GraphIterator(resultList.iterator());
}
58
Graph Traversals: Example

 Example: Consider this graph

59
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal

 Enqueue the first vertex onto the queue

A
60
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal

 Dequeue A and Enqueue B, C and E

B C E
61
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue B and Enqueue D

A, B

C E D
62
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue C and Enqueue F

A, B, C

E D F
63
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue E and Enqueue G and H

A, B, C, E

D F G H
64
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue D

A, B, C, E, D

F G H
65
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue F

A, B, C, E, D, F

G H
66
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue G and Enqueue I

A, B, C, E, D, F, G

H I
67
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue H

A, B, C, E, D, F, G, H

I
68
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 Dequeue I

A, B, C, E, D, F, G, H, I

69
Graph Traversals: Example (cont...)

 Performing a breadth-first traversal:

 The queue is empty: we are finished

A, B, C, E, D, F, G, H, I

70
Graph Traversals: Example (cont...)

 Perform a recursive depth-first traversal on this


same graph

71
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

 Visit the first node

72
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

 A has an unvisited neighbor

A, B

73
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

B has an unvisited neighbor

A, B, C

74
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

C has an unvisited neighbor

A, B, C, D

75
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

D has no unvisited neighbors, so we return to C

A, B, C, D, E

76
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

E has an unvisited neighbor

A, B, C, D, E, G

77
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

G has an unvisited neighbor

A, B, C, D, E, G, I

78
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

I has an unvisited neighbor

A, B, C, D, E, G, I, H

79
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

 We recurse back to C which has an unvisited


neighbour

A, B, C, D, E, G, I, H, F

80
Graph Traversals: Example (cont...)

 Performing a recursive depth-first traversal:

 We recurse finding that no other nodes have


unvisited neighbours

A, B, C, D, E, G, I, H, F

81
Shortest Path

 There are two possibilities for determining the


“shortest” path in a graph
 determine the literal shortest path between a starting vertex
and a target vertex (the least number of edges between the
two vertices)
 simplest approach – a variation of the breadth-first traversal
algorithm

 look for the cheapest path in a weighted graph


 use a minheap or a priority queue storing vertex, weight pairs

82
Shortest Path (cont…)

 Given a weighted directed graph, one common


problem is finding the shortest path between two
given vertices
 Recall that in a weighted graph, the length of a path is the
sum of the weights of each of the edges in that path

83
Shortest Path - Algorithms

 Algorithms for finding the shortest path include:


 Dijkstra’s algorithm

 A* search algorithm

 Bellman-Ford algorithm

 …

84
Dijkstra’s Algorithm

 The goal of this algorithm will be to find the shortest path and its length

 Dijkstra’s algorithm works on graphs where the weights on all edges is


positive

 It finds the shortest path from an initial vertex, say s, to all the other
vertices.

 We will make the assumption that the weights on all edges is a positive
number
 Clearly, if we have negative vertices, it may be possible to end up in a cycle
whereby each pass through the cycle decreases the total length

 Thus, a shortest length would be undefined for such a graph

85
Dijkstra’s Algorithm - Algorithm Steps

86
Dijkstra’s algorithm - Example 1

87
Dijkstra’s algorithm - Example 1 (cont…)

88
Dijkstra’s algorithm - Example 1 (cont…)

89
Dijkstra’s algorithm - Example 1 (cont…)

90
Dijkstra’s algorithm - Example 1 (cont…)

91
Dijkstra’s algorithm - Example 1 (cont…)

92
Dijkstra’s algorithm - Example 1 (cont…)

93
Dijkstra’s algorithm - Example 1 (cont…)

94
Dijkstra’s algorithm - Example 2

 Tracing Dijkstra’s algorithm starting at vertex B:

The resulting vertex-weighted graph is:

95
Running Time of Some Graph Algorithms

96
Summary
 A path is a sequence of edges that connects two vertices in a graph.

 The length of a path in is the number of edges in the path.

 A cycle is a path in which the first and last vertices are the same and none of the edges are repeated.

 A graph that has no cycles is called acyclic.

 A graph is connected if there exists a path between any two vertices.

 An undirected graph is a graph where the pairings representing the edges are unordered.

 A directed graph, sometimes referred to as a digraph, is a graph where the edges are ordered pairs of
vertices.

 A weighted graph, sometimes called a network, is a graph with weights (or costs) associated with
each edge.

 Weighted graphs may be either undirected or directed.

 A path in an undirected graph is an ordered sequence of vertices.

 An undirected graph is considered connected if for any two vertices in the graph there is a path
between them.
97
Summary (cont…)

 Path and cycles in a digraph: must move in the direction specified by the arrow.

 Connectedness in a digraph: strong and weak.

 Strongly Connected: If connected as a digraph - following the arrows.

 Weakly connected: If the underlying undirected graph is connected (i.e. ignoring the arrows).

 The length of a path within a weighted graph is the sum of all of the edges which make up the path.

 There are two main types of graph traversal algorithms: breadth-first (requires a queue), and
depth-first (requires a stack).
 Given a weighted directed graph, we can find the shortest distance between two vertices by:

 starting with a trivial path containing the initial vertex

 growing this path by always going to the next vertex which has the shortest current path

 Dijkstra's algorithm solves the single-source shortest path problem for a non-negative weights
graph.

 Strategies for implementing graphs: Adjacency matrices and Adjacency lists.

98
References

 Java Software Structures - Designing and Using Data Structures, 4th


edition, Lewis and Chase.

 Data Structures & Algorithms in Java, 4th edition, Drozdek.

 Data Structures and Problem Solving Using Java, 4th edition, Weiss.

 Slides at: https://ece.uwaterloo.ca/~dwharder/aads/Lecture_materials/

Douglas Wilhelm Harder, Mmath, University of Waterloo.

 Slides at: http://faculty.kfupm.edu.sa/ICS/jauhar/ics202/

99
Any Question
???

100

You might also like