CS211 8 Graphs
CS211 8 Graphs
CS211
Graphs
Lectures
16, 17 & 18
Samar Alsaleh
Spring 2022
04
03 Finishing Thoughts
Summary & resources
02 Shortest Path
Problem definition, applications, & alg
Connected Graph
- A graph is complete if every pair of dis nct ver ces is connected
by a unique edge
- A graph is connected if there is a path from each vertex to every
other vertex
- A sub-graph of a graph is a subset of the ver ces and a subset of
Sub-Graph the edges that connects the subset of ver ces in the original graph
(of the two previous graphs)
5
ti
ti
ti
ti
ti
ti
ti
ti
ti
Graph Terminology (cont.)
- All together and some mores…
Anomalies:
connected
sup-graphs
6
Graph ADT Types
A connected An unconnected - Two ver ces vi, vj are connected if there exists a path from
graph graph vi to vj
9
ti
ti
ti
fi
ft
ti
Direct Graph
- A directed graph, some mes referred to as a digraph,
is a graph where the edges are ordered pairs of ver ces
- This means that the edges (A, B) and (B, A) are
separate, direc onal edges in a digraph
- Edges on a digraph are associated with a direc on
- Edges are ordered pairs (vj, vk) deno ng a connec on
from vj to vk
- The edge (vj, vk) is di erent from the edge (vk, vj)
- Example: Streets are represented by digraphs
- In most cases, you can go two ways unless it is a one-
way street
10
ti
ff
ti
ti
ti
ti
ti
Direct Graph (cont.)
Example digraphs:
- The digraph on the top le has:
- Four ver ces: V = {A, B, C, D}
- Five directed edges:
E = {(A, B), (A, D), (B, C), (B, D), (D, C)}
11
ti
ti
tt
ft
ft
Direct Graph (cont.)
12
ti
ti
ti
ti
fi
Direct Graph (cont.)
- Sources and Sinks
- Ver ces with an in-degree of zero are described as sources
- Ver ces with an out-degree of zero are described as sinks
- In the top le graph:
- Sources: v1, v6, v7
- Sinks: v2, v9
- Connec vity
- A digraph is strongly connected if it contains a directed
path for every pair of ver ces {vi, vj}
- A digraph is weakly connected - or just connected - if
Strongly Weakly replacing all its directed edges with undirected edges
Connected Connected produces a connected (undirected) graph
13
ti
ti
ti
ft
ti
Direct Graph (cont.)
Cyclic Digraph Acyclic Digraph
- Acyclic Digraph (DAG)
- In a simple path, the verces and edges
are dis nct except that the rst and last
vertex may be the same
- If only the
rst and last ver ces are the
same, the path is called a cycle
More Acyclic Digraphs - A graph that has no cycles is called acyclic
- A digraph that has no cycles is called
acyclic digraph (DAG)
- Is a tree an example of DAG?! Explain!
14
ti
fi
ti
ti
fi
Weighted Graphs
15
ft
ti
tt
ti
ti
Weighted Graphs (cont.)
Weighted Graph Weighted Digraph
6.4
7.5 6.8
6.7 7.3
5.9
4.1
3.2
4.7
5.4 4.5
- Electronic circuits
- Printed circuit board
- Integrated circuit
- Transporta on networks
- Highway network
- Flight network
- Computer networks
- Local area network
- Internet/Web
- Databases
- En ty-rela onship diagram
17
ti
ti
ti
ti
Graphs Applica ons (cont.)
Graph Vertix Edge
communication telephone, computer fiber optic cable
circuit gate, register, processor wire
mechanical joint rod, beam, spring
financial stock, currency transactions
transportation intersection street
internet class C network connection
game board position legal move
social relationship person friendship
neural network neuron synapse
protein network protein protein-protein interaction
molecule atom bond
18
ti
Graphs Applica ons (cont.)
- Some example applica ons of Graph:
- Finding the least congested route between two phones, given connec ons between
switching sta ons
- Determining if there is a way to get from one page to another on the WWW, just by
following links
- While driving, nd the shortest path from one city to another
- Finding the shortest path that includes all the ci es for a traveling salesperson who has to
visit a number of ci es
- Determine an ordering of courses so that you always take prerequisite courses rst
19
ti
fi
ti
ti
ti
ti
ti
fi
02
21
ti
ti
ti
fi
ti
ti
ti
fi
ti
Graph Representa ons
Two representa ons of graphs are most common:
1. Adjacency List
- Edges are represented by an array of lists, called adjacency lists, where each list stores
the ver ces adjacent to a par cular vertex
2. Adjacency Matrix
- Edges are represented by a two dimensional array, called an adjacency matrix
22
ti
ti
ti
ti
Adjacency List
1 class Vertex {
2 dataType data;
3 boolean visited;
4 LinkedList neighbors;
5 : : :
6 }
23
Adjacency Matrix
24
Adjacency List or Adjacency Matrix?
- In terms if worst case complexity:
Adjacency List Adjacency Matrix
Space |E| + |V | | V |2
Adding an edge 1 1
Checking if there is an
degree(v) 1
edge between v and w
Iterating over vertices
Ac vity: degree(v) |V |
adjacent to v
- Write down both adjacency matrix
and adjacency list representa ons - In prac ce, use adjacency-lists representa on
for this graph. - Algorithms based on itera ng over ver ces adjacent to v
- Which one is be er for this graph? - Real-world graphs tend to be sparse
Why? - huge number of ver ces, small average vertex degree
25
ti
ti
tt
ti
ti
ti
ti
ti
Graph Traversals
- Traversals of graphs are also called searches
- There are two main types of graph traversal algorithms:
1. Breadth- rst: behaves much like a level-order traversal of a tree
- Breadth- rst requires a queue
2. Depth- rst: behaves much like the preorder traversal of a tree
- Depth- rst requires a stack
- One di erence: there is no root node present in a graph
- Graph traversals may start at any vertex in the graph
- Let’s assume that if there is ever a choice amongst mul ple nodes, both the BFS and
DFS algorithms will choose the le -most node rst
26
fi
fi
ff
fi
fi
ft
fi
ti
Breadth-First Search (BFS)
- Conceptually, in BFS, we visit the start vexerrst, then all ver ces that are adjacent to
it next, then all nodes that can be reached by a path from the start node containing
two edges, three edges, and so on
- It’s important to note that:
- BFS computes shortest paths (fewest number of edges) from the start vertex s to all other
ver ces in a graph
- Must visit all nodes for which the shortest path from the start node is length k before we
visit any node for which the shortest path from the start node is length k + 1
- There is no special start vertex
27
ti
fi
ti
Breadth-First Search (BFS) - Algorithm
- As men oned earlier, implemen ng breadth- rst search requires a queue
- The following is an example algorithm for BFS:
28
ti
ti
fi
Breadth-First Search (BFS) - Example
(Input Graph)
5 5 5 5
0 1
0 4 0 4 0 4 0 4
0 1 3
3 2
3 1 6
➡ 3 1 6
➡ 3 1 6
➡ 4
3 1 6
2 7 2 7 2 7 6 2 7
7
9 8 9 8 9 8 9 8
5 5
3 2 4 6 7 8 9 5
0 4 0 4
2 4 6 7 8 9 5
➡ 4
6
6
7
3 1 6
➡ 7
8
3 1 6
➡ 8
9
9
5
5
7 8 2 7 9 2 7 5
9 5
9 8 9 8 Done! 29
Breadth-First Search (BFS) - Example (cont.)
- The following is a summary of the sequence of operaons in the previous example
a er the star ng vertex has been enqueued and visited:
Dequeued vertex Enqueued/visisted vertices Queue content Visit sequence
0 0 0
0 1, 3 1, 3 0, 1, 3
1 2, 4, 6, 7 3, 2, 4, 6, 7 0, 1, 3, 2, 4, 6, 7
3 2, 4, 6, 7 0, 1, 3, 2, 4, 6, 7
2 8, 9 4, 6, 7, 8, 9 0, 1, 3, 2, 4, 6, 7, 8, 9
4 5 6, 7, 8, 9, 5 0, 1, 3, 2, 4, 6, 7, 8, 9, 5
6 7, 8, 9, 5
7 8, 9, 5
8 9, 5
9 5
5 empty
30
ft
ti
ti
Depth-First Search (DFS)
- Conceptually, in DFS, you start at a vertex, visit it, and choose one adjacent vertex to
visit; then, choose a vertex adjacent to that vertex to visit, and so on un l you go no
further; then back up and see whether a new vertex can be found
- As men oned earlier, implemen ng depth- rst search requires a stack
- The following is an example algorithm for a recursive DFS (u lizing the recursion
stack):
1 DFS (to visit a vertex v)
2 Mark v as visited
3 Recursively visit all unmarked vertices adjacent to v
31
ti
ti
fi
ti
ti
Depth-First Search (DFS) (cont.)
- The following is an example algorithm for an itera ve DFS (u lizing a user de ned
stack ):
32
ti
ft
ti
ti
fi
Depth-First Search (DFS) - Example
(Input Graph)
0 0 0 1 0 3 0
0 1
1 2 1 2 1 2 0 1 2
➡ ➡ ➡
3 4 5 6 3 4 5 6 3 4 5 6 3 4 5 6
4 0 4 0 3 0 1 0
3 4 3 1 0
1 1 2 3 1 1 2 0 1 2 1 2
➡ 0 ➡ 1 0 ➡ ➡
3 4 5 6 0 3 4 5 6 3 4 5 6 3 4 5 6
33
Depth-First Search (DFS) - Example (cont.)
2 0 5 0 6 0 6 0
0 2 5 6 5
1 2 1 2 1 2 1 2
➡ ➡ 0
➡ 2
0 ➡ 5
2
2
0
3 4 5 6 3 4 5 6 3 4 5 6 0 3 4 5 6
5 0 2 0 0 0
2 0
1 2 1 2 1 2
➡ 0
➡ ➡
3 4 5 6 3 4 5 6 3 4 5 6
Done!
34
Depth-First Search (DFS) - Example (cont.)
- The following is a summary of the sequence of opera ons in the previous example
a er the rst visit func on call (note: the stack here is the recursion stack):
Pushed vertex Poped/visisted veritx Stack content Visit sequence
0 0
1 0, 1
3 0, 1, 3
4 0, 1, 3, 4
4 0, 1, 3 4
3 0, 1 4, 3
1 0 4, 3, 1
2 0, 2
5 0, 2, 5
6 0, 2, 5, 6
6 0, 2, 5 4, 3, 1, 6
5 0, 2 4, 3, 1, 6, 5
2 0 4, 3, 1, 6, 5, 2
0 empty 4, 3, 1, 6, 5, 2, 0 35
ft
fi
ti
ti
Graph Traversal Example
- Travers the following graph using both BFS and DFS:
D
H
B I
E
A G
C F
36
Graph Traversal Example (cont.)
Performing a breadth- rst traversal:
D D D
H H H
B I B I B I
E E E
A G A G A G
C F C F C F
➡ ➡
A E C B A D E C B
D D D
H H H
B I B I B I
E E E
A G A G A G
C F C F C F
➡ ➡ ➡
F D E C H G F D E H G F D
38
fi
Graph Traversal Example (cont.)
Performing a breadth- rst traversal (cont.):
D D D
H H H
B I B I B I
E E E
A G A G A G
C F C F C F
➡ ➡ ➡
H G F I H G I H
D D
H H
B I B I
E E
A G A G
C F C F
➡ ➡
I
40
fi
Graph Traversal Example (cont.)
Performing a recursive depth- rst traversal:
D D D
H H H
B I B I B I
E E E
A G A G A G
C F C F C F
➡ ➡
A A B A B C
D D D
H H H
B I B I B I
E E E
A G A G A G
C F C F C F
➡ ➡ ➡
A B C D A B C D A B C D E
D D D
H H H
B I B I B I
E E E
A G A G A G
C F C F C F
➡ ➡ ➡
A B C D E G A B C D E G I A B C D E G I H
D D D
H H H
B I B I B I
E E E
A G A G A G
C F C F C F
➡ ➡ ➡
A B C D E G I H A B C D E G I H F A B C D E G I H F
➔ BFS will visit 5 nodes prior to the red one, while DFS will visit 10 nodes prior to the red one
➔ Remember that BFS behaves like level-order traversal of a tree while DFS like preorder traversal 45
ti
ti
ti
More Examples
- Check out more examples with interac ve visual illustra on:
<https://www.cs.usfca.edu/~galles/visualization/BFS.html> <https://www.cs.usfca.edu/~galles/visualization/DFS.html>
46
ti
ti
03
02 Shortest Path
Problem definition, applications, & alg
48
ti
fi
ti
ti
ti
Shortest Path Problem (cont.)
- There are two possibili es for determining the “shortest” path in a graph
1. Determine the literal shortest path between two ver ces
- That is, the least number of edges between the star ng vertex and the target vertex
- Simplest approach – a varia on of the breadth- rst traversal algorithm
2. Look for the cheapest path in a weighted graph
- 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
- Possible approach – a min-heap or a priority queue storing (vertex, weight) pairs
49
ti
ti
fi
ti
ti
Shortest Path Applica ons
50
ti
ti
fi
ti
fi
ti
ti
fi
ti
Shortest Path Algorithms
- The most important algorithms for solving the shortest path problem include:
- Dijkstra’s algorithm
- A* search algorithm
- Bellman-Ford algorithm
-…
- Out of which, Dijkstra’s algorithm might be the most famous one for nding the
shortest path in non-nega ve edge-weighted graphs
51
ti
fi
Dijkstra’s Algorithm
- The goal of this algorithm is to nd the shortest path and its length
- Dijkstra’s algorithm works on graphs where the weights on all
edges is posi ve
- It nds the shortest path from an ini al vertex, say s, to all the
other ver ces
- We will make the assump on that the weights on all edges is a
posi ve number
Dijkstra. Dijkstra, - Clearly, if we have nega ve ver ces, it may be possible to end up in a
recipient of the 1972
ACM Turing Award
cycle whereby each pass through the cycle decreases the total length
- Thus, a shortest length would be unde ned for such a graph
52
fi
ti
ti
ti
ti
ti
ti
fi
fi
ti
Dijkstra’s Algorithm (cont.)
53
ti
ti
ft
fi
Dijkstra’s Algorithm (cont.)
- For each vertex v, Dijkstra’s algorithm keeps track of three pieces of informa on:
1. A boolean telling whether we know the shortest path to that node (ini ally true only for
the star ng node)
2. The length of the shortest path to that node known so far (0 for the star ng node)
3. The predecessor of that node along the shortest known path (unknown for all nodes)
- Using this informa on, Dijkstra’s algorithm proceeds in phases—at each step:
1. From the ver ces for which we don’t know the shortest path, pick a vertex v with the
smallest distance known so far
2. Set v’s “known” eld to true
3. For each vertex w adjacent to v, test whether its distance so far is greater than v’s
distance plus the distance from v to w; if so, set w’s distance to the new distance and w’s
predecessor to v
54
ti
ti
fi
ti
ti
ti
ti
Dijkstra’s Algorithm - Example 1
55
Dijkstra’s Algorithm - Example 1 (cont.)
56
Dijkstra’s Algorithm - Example 1 (cont.)
57
Dijkstra’s Algorithm - Example 1 (cont.)
58
Dijkstra’s Algorithm - Example 1 (cont.)
59
Dijkstra’s Algorithm - Example 1 (cont.)
60
Dijkstra’s Algorithm - Example 1 (cont.)
61
Dijkstra’s Algorithm - Example 1 (cont.)
62
Dijkstra’s Algorithm - Example 2
- Tracing Dijkstra’s algorithm star ng at vertex B:
63
ti
ti
More Examples
- Check out more examples with interac ve visual illustra on:
<https://www.cs.usfca.edu/~galles/visualization/Dijkstra.html>
64
ti
ti
Running Time of Graph Algorithms
65
04
03 Finishing Thoughts
Summary & resources
02 Shortest Path
Problem definition, applications, & alg
67
Summary
A path is a sequence of edges that connects two ver ces 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 rst and last ver ces 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 ver ces.
An undirected graph is a graph where the pairings represen ng the edges
are unordered.
A digraph, some mes referred to as a digraph, is a graph where the edges
are ordered pairs of ver ces.
A weighted graph, some mes called a network, is a graph with weights
(or costs) associated with each edge.
Weighted graphs may be either undirected or directed.
68
ti
ti
ti
fi
ti
ti
ti
ti
Summary (cont.)
A path in an undirected graph is an ordered sequence of ver ces.
An undirected graph is considered connected if for any two ver ces in the
graph there is a path between them.
Path and cycles in a digraph: must move in the direc on speci ed 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.
Strategies for implemen ng graphs: Adjacency matrices and Adjacency lists.
There are two main types of graph traversal algorithms: breadth- rst
(requires a queue), and depth- rst (requires a stack).
69
ti
fi
ti
ti
fi
ti
fi
Summary (cont.)
70
ti
ti
ti
fi
ti
Resources & More…
Textbooks Chapters:
- Data Structures and Algorithms in Java, Ch8
- Data Structures and Algorithm Analysis in Java, Ch9
Interesting Articles & Videos:
- Graph Data Structure Intro (short video)
- Graph Data Structure 1 (short video)
- Graphs — A Visual Introduction for Beginners (article)
- How Dijkstra's Algorithm Works (video)
- Dijkstra's Shortest Path Algorithm - A Detailed and Visual
Introduction (article)
Credits:
- Some slides content and illustrations are courtesy of
Prof. Robert Sedgewick, Princeton University 71
Resources & More…
Other Resources:
- Java Software Structures - Designing and Using Data Structures,
4th edition, Lewis and Chase.
- Data Structures & Algorithms in Java, 4th edition, Drozdek.
- Data Structures and Algorithms in Java, 6th edition, Goodrich,
Tamassia and Goldwasser.
- 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/
72
Thank You & Stay Safe
:)