Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for...
Transcript of Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for...
![Page 1: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/1.jpg)
Introduction to Graphs http://people.cs.clemson.edu/~pargas/courses/cs212/common/notes/ppt/
![Page 2: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/2.jpg)
Slide 2
Introduction
• Graphs are a generalization of trees – Nodes or verticies – Edges or arcs
• Two kinds of graphs – Directed – Undirected
![Page 3: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/3.jpg)
Slide 3
Introduction: Formal Definition
• A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges E.
• Each edge is a pair (v,w) where v, w ∈ V
![Page 4: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/4.jpg)
Slide 4
Introduction: Formal Definition
• A directed graph, or digraph, is a graph in which the edges are ordered pairs – (v, w) ≠ (w, v)
• An undirected graph is a graph in which the edges are unordered pairs – (v, w) == (w, v)
![Page 5: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/5.jpg)
Slide 5
Introduction: Directed Graphs
• In a directed graph, the edges are arrows. • Directed graphs show the flow from one node
to another and not vise versa.
![Page 6: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/6.jpg)
Slide 6
Introduction: Undirected Graphs
• In a directed graph, the edges are lines. • Directed graphs show a relationship between
two nodes.
![Page 7: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/7.jpg)
Slide 7
Terminology
• In the directed graph above, b is adjacent to a because (a, b) ∈ E. Note that a is not adjacent to b.
• A is a predecessor of node B • B is a successor of node A • The source of the edge is node A, the target is node
B
![Page 8: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/8.jpg)
Slide 8
Terminology
• In the undirected graph above, a and b are adjacent because (a,b) ∈ E. a and b are called neighbors.
![Page 9: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/9.jpg)
Slide 9
Terminology
• A path is a sequence of vertices w1, w2,…wn such that (wi, wi+1) ∈ E, 1 <= i < n, and each vertex is unique except that the path may start and end on the same vertex
• The length of the path is the number of edges along the path
![Page 10: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/10.jpg)
Slide 10
Terminology
• An acyclic path is a path where each vertex is unique
• A cyclic path is a path such that – There are at least two vertices on the path – w1 = wn (path starts and ends at same vertex)
![Page 11: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/11.jpg)
Slide 11
Test Your Knowledge Cyclic or Acyclic?
![Page 12: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/12.jpg)
Slide 12
Terminology
• A directed graph that has no cyclic paths is called a DAG (a Directed Acyclic Graph).
• An undirected graph that has an edge between every pair of vertices is called a complete graph.
Note: A directed graph can also be a complete graph; in that case, there must be an edge from every vertex to every other vertex.
![Page 13: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/13.jpg)
Slide 13
Test Your Knowledge Complete, or “Acomplete” (Not Complete)
![Page 14: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/14.jpg)
Slide 14
Test Your Knowledge Complete, or “Acomplete” (Not Complete)
![Page 15: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/15.jpg)
Slide 15
Terminology
• An undirected graph is connected if a path exists from every vertex to every other vertex
• A directed graph is strongly connected if a path exists from every vertex to every other vertex
• A directed graph is weakly connected if a path exists from every vertex to every other vertex, disregarding the direction of the edge
![Page 16: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/16.jpg)
Slide 16
Test Your Knowledge Connected, Strongly connected, or Weakly
connected
![Page 17: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/17.jpg)
Slide 17
Terminology
• A graph is known as a weighted graph if a weight or metric is associated with each edge.
![Page 18: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/18.jpg)
Slide 18
Various types of graphs
• Connected/disconnected graphs
• The circled subgraphs are also known as connected components
![Page 19: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/19.jpg)
Slide 19
Various types of graphs
• Directed/undirected graphs
• You may treat each undirected edge as two directed edges in opposite directions
![Page 20: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/20.jpg)
Slide 20
Various types of graphs
• Weighted/unweighted graphs
• You may treat unweighted edges to be weighted edges of equal weights
5 -2 7
4
0
![Page 21: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/21.jpg)
Slide 21
Special graphs
• Planar graphs – A graph that can be drawn on a plane without edge
intersections – The following two graphs are equivalent and
planar:
• To be discussed in details in Graph (III) J
![Page 22: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/22.jpg)
Slide 22
Special graphs
• Tree: either one of the followings is the definition – A connected graph with |V|-1 edges – A connected graph without cycles – A graph with exactly one path between every pair
of vertices
![Page 23: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/23.jpg)
Slide 23
Special graphs
• Tree edges could be directed or undirected • For trees with directed edges, a root usually
exists
![Page 24: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/24.jpg)
Slide 24
Special graphs
• Forest – All connected component(s) is/are tree(s)
• How many trees are there in the following forest?
![Page 25: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/25.jpg)
Slide 25
How to store graphs in the program?
• Usually, the vertices are labeled beforehand • 3 types of graph representations:
– Adjacency matrix – Adjacency list – Edge list
1 3
4
2 5
5 -2 7
4
0
![Page 26: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/26.jpg)
Slide 26
Adjacency matrix
• Use a 2D array 1 3
4
2 5
5 -2 7
4
0 s\t 1 2 3 4 5
1 5 -‐2
2 0 7
3
4 4
5
![Page 27: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/27.jpg)
Slide 27
Adjacency matrix
• Memory complexity? • Time complexity for:
– Checking the weight of an edge between 2 given nodes?
– Querying all adjacent nodes of a given node?
![Page 28: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/28.jpg)
Slide 28
Adjacency list
• N vertices, N linked lists • Each list stores its adjacent vertices
1 3
4
2 5
5 -2 7
4
0
1 5|-2
3|5
2 1|0 3|7
3
4 2|4
5
![Page 29: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/29.jpg)
Slide 29
Adjacency list
• Memory complexity? • Time complexity for:
– Checking the weight of an edge between 2 given nodes?
– Querying all adjacent nodes of a given node?
![Page 30: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/30.jpg)
Slide 30
Edge list
• A list of edges 1 3
4
2 5
5 -2 7
4
0 id x y w
0 1 5 -‐2
1 2 1 0
2 1 3 5
3 2 3 7
4 4 2 4
![Page 31: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/31.jpg)
Slide 31
Edge list
• Memory complexity? • Time complexity for:
– Checking the weight of an edge between 2 given nodes?
– Querying all adjacent nodes of a given node?
![Page 32: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/32.jpg)
Slide 32
Which one should be used?
• It depends on: – Constraints – Time Limit – Memory Limit – What algorithm is used
![Page 33: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/33.jpg)
Slide 33
Uses for Graphs
• Precedence Constraints: Suppose you have a set of jobs to complete, but some must be completed before others are begun. (For example, Atilla advises you always pillage before you burn.) Here the vertices are jobs to be done. Directed edges indicate constraints; there is a directed edge from job u to job v if job u must be done before job v is begun.
![Page 34: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/34.jpg)
Slide 34
Uses for Graphs
• Two-Player Game Tree: All of the possibilities in a board game like chess can be represented in a graph. Each vertex stands for one possible board position. (For chess, this is a very big graph!)
![Page 35: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/35.jpg)
Slide 35
Uses for Graphs
• Computer network: The set of vertices V represents the set of computers in the network. There is an edge (u, v) if and only if there is a direct communication link between the computers corresponding to u and v.
![Page 36: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/36.jpg)
Slide 36
Topological Sort
Don’t burn before you pillage!
![Page 37: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/37.jpg)
Slide 37
Topological Sort
• Informally, a topological sort is a linear ordering of the vertices of a DAG in which all successors of any given vertex appear in the sequence after that vertex.
![Page 38: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/38.jpg)
Slide 38
Method to the Madness
• One way to find a topological sort is to consider the in-degrees of the vertices. (The number of incoming edges is the in-degree). Clearly the first vertex in a topological sort must have in-degree zero and every DAG must contain at least one vertex with in-degree zero.
![Page 39: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/39.jpg)
Slide 39
Simple Topological Sort Algorithm
• Repeat the following steps until the graph is empty: – Select a vertex that has in-degree zero. – Add the vertex to the sort. – Delete the vertex and all the edges emanating from
it from the graph.
![Page 40: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/40.jpg)
Slide 40
Test Your Knowledge
• Give a topological sort for this graph, it should be evident that more than one solution exists for this problem.
![Page 41: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/41.jpg)
Slide 41
Backtracking Algorithm Depth-First Search
Text Read Weiss, § 9.6 Depth-First Search and §
10.5 Backtracking Algorithms
![Page 42: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/42.jpg)
Slide 42
Requirements
• Also called Depth-First Search • Can be used to attempt to visit all nodes of a
graph in a systematic manner • Works with directed and undirected graphs • Works with weighted and unweighted graphs
![Page 43: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/43.jpg)
Slide 43
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C D E F G H
Task: Conduct a depth-first search of the graph starting with node D
![Page 44: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/44.jpg)
Slide 44
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C D √E F G H
Visit D
D The order nodes are visited:
D
![Page 45: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/45.jpg)
Slide 45
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C D √E F G H
Consider nodes adjacent to D, decide to visit C first (Rule:
visit adjacent nodes in alphabetical order)
D The order nodes are visited:
D
![Page 46: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/46.jpg)
Slide 46
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E F G H
Visit C
C
D The order nodes are visited:
D, C
![Page 47: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/47.jpg)
Slide 47
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E F G H
No nodes adjacent to C; cannot continue è backtrack, i.e.,
pop stack and restore previous state
C
D The order nodes are visited:
D, C
![Page 48: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/48.jpg)
Slide 48
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E F G H
Back to D – C has been visited, decide to visit E next
D The order nodes are visited:
D, C
![Page 49: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/49.jpg)
Slide 49
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E √F G H
Back to D – C has been visited, decide to visit E next
E
D The order nodes are visited:
D, C, E
![Page 50: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/50.jpg)
Slide 50
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E √F G H
Only G is adjacent to E
E
D The order nodes are visited:
D, C, E
![Page 51: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/51.jpg)
Slide 51
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E √F G √H
Visit G
G
E
D The order nodes are visited:
D, C, E, G
![Page 52: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/52.jpg)
Slide 52
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E √F G √H
Nodes D and H are adjacent to G. D has already been
visited. Decide to visit H.
G
E
D The order nodes are visited:
D, C, E, G
![Page 53: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/53.jpg)
Slide 53
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E √F G √H √
Visit H
H
G
E
D The order nodes are visited:
D, C, E, G, H
![Page 54: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/54.jpg)
Slide 54
A
H B
F
E
D
C
G
Walk-Through Visited Array A
B C √D √E √F G √H √
Nodes A and B are adjacent to F. Decide to visit A next.
H
G
E
D The order nodes are visited:
D, C, E, G, H
![Page 55: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/55.jpg)
Slide 55
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B C √D √E √F G √H √
Visit A
A
H
G
E
D The order nodes are visited:
D, C, E, G, H, A
![Page 56: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/56.jpg)
Slide 56
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B C √D √E √F G √H √
Only Node B is adjacent to A. Decide to visit B next.
A
H
G
E
D The order nodes are visited:
D, C, E, G, H, A
![Page 57: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/57.jpg)
Slide 57
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F G √H √
Visit B
B
A
H
G
E
D The order nodes are visited:
D, C, E, G, H, A, B
![Page 58: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/58.jpg)
Slide 58
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F G √H √
No unvisited nodes adjacent to B. Backtrack (pop the stack).
A
H
G
E
D The order nodes are visited:
D, C, E, G, H, A, B
![Page 59: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/59.jpg)
Slide 59
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F G √H √
No unvisited nodes adjacent to A. Backtrack (pop the stack).
H
G
E
D The order nodes are visited:
D, C, E, G, H, A, B
![Page 60: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/60.jpg)
Slide 60
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F G √H √
No unvisited nodes adjacent to H. Backtrack (pop the
stack).
G
E
D The order nodes are visited:
D, C, E, G, H, A, B
![Page 61: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/61.jpg)
Slide 61
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F G √H √
No unvisited nodes adjacent to G. Backtrack (pop the
stack).
E
D The order nodes are visited:
D, C, E, G, H, A, B
![Page 62: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/62.jpg)
Slide 62
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F G √H √
No unvisited nodes adjacent to E. Backtrack (pop the stack).
D The order nodes are visited:
D, C, E, G, H, A, B
![Page 63: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/63.jpg)
Slide 63
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F G √H √
F is unvisited and is adjacent to D. Decide to visit F next.
D The order nodes are visited:
D, C, E, G, H, A, B
![Page 64: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/64.jpg)
Slide 64
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F √G √H √
Visit F
F
D The order nodes are visited:
D, C, E, G, H, A, B, F
![Page 65: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/65.jpg)
Slide 65
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F √G √H √
No unvisited nodes adjacent to F. Backtrack.
D The order nodes are visited:
D, C, E, G, H, A, B, F
![Page 66: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/66.jpg)
Slide 66
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F √G √H √
No unvisited nodes adjacent to D. Backtrack.
The order nodes are visited:
D, C, E, G, H, A, B, F
![Page 67: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/67.jpg)
Slide 67
A
H B
F
E
D
C
G
Walk-Through Visited Array A √
B √C √D √E √F √G √H √
Stack is empty. Depth-first traversal is done.
The order nodes are visited:
D, C, E, G, H, A, B, F
![Page 68: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/68.jpg)
Slide 68
Consider Trees
1. What depth-first traversals do you know?
2. How do the traversals differ?
3. In the walk-through, we visited a node just as we pushed the node onto the stack. Is there another time at which you can visit the node?
4. Conduct a depth-first search of the same graph using the strategy you came up with in #3.
![Page 69: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/69.jpg)
Slide 69
Breadth-First Search
Text Read Weiss, § 9.3 (pp. 299-304) Breadth-First
Search Algorithms
![Page 70: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/70.jpg)
Slide 70
Requirements
• Can be used to attempt to visit all nodes of a graph in a systematic manner
• Works with directed and undirected graphs • Works with weighted and unweighted graphs
![Page 71: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/71.jpg)
Slide 71
A
H B
F
E
D
C
G
Overview
Task: Conduct a breadth-first search of the graph starting with node D
Breadth-first search starts with given node
0
![Page 72: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/72.jpg)
Slide 72
A
H B
F
E
D
C
G
Overview
Nodes visited: D
Breadth-first search starts with given node
Then visits nodes adjacent in some specified order (e.g., alphabetical)
Like ripples in a pond 0
1
![Page 73: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/73.jpg)
Slide 73
A
H B
F
E
D
C
G
Overview
Nodes visited: D, C
Breadth-first search starts with given node
Then visits nodes adjacent in some specified order (e.g., alphabetical)
Like ripples in a pond 0
1
![Page 74: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/74.jpg)
Slide 74
A
H B
F
E
D
C
G
Overview
Nodes visited: D, C, E
Breadth-first search starts with given node
Then visits nodes adjacent in some specified order (e.g., alphabetical)
Like ripples in a pond 0
1
![Page 75: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/75.jpg)
Slide 75
A
H B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F
Breadth-first search starts with given node
Then visits nodes adjacent in some specified order (e.g., alphabetical)
Like ripples in a pond 0
1
![Page 76: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/76.jpg)
Slide 76
A
H B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G
When all nodes in ripple are visited, visit nodes in next ripples
0
2 1
![Page 77: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/77.jpg)
Slide 77
A
H B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G, H
When all nodes in ripple are visited, visit nodes in next ripples
0
2 1
3
![Page 78: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/78.jpg)
Slide 78
A
H B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G, H, A
When all nodes in ripple are visited, visit nodes in next ripples
0
2 1
3
4
![Page 79: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/79.jpg)
Slide 79
A
H B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G, H, A, B
When all nodes in ripple are visited, visit nodes in next ripples
0
2 1
3
4
![Page 80: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/80.jpg)
Slide 80
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A
B C D E F G H
How is this accomplished? Simply replace the stack with a queue! Rules: (1) Maintain an enqueued array. (2) Visit node when dequeued.
Q à
![Page 81: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/81.jpg)
Slide 81
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A
B C D √ E F G H
Enqueue D. Notice, D not yet visited.
Q à D
Nodes visited:
![Page 82: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/82.jpg)
Slide 82
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A
B C √ D √ E √ F √ G H
Dequeue D. Visit D. Enqueue unenqueued nodes adjacent to D.
Q à C à E à F
Nodes visited: D
![Page 83: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/83.jpg)
Slide 83
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A
B C √ D √ E √ F √ G H
Dequeue C. Visit C. Enqueue unenqueued nodes adjacent to C.
Q à E à F
Nodes visited: D, C
![Page 84: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/84.jpg)
Slide 84
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A
B C √ D √ E √ F √ G H
Dequeue E. Visit E. Enqueue unenqueued nodes adjacent to E.
Q à F à G
Nodes visited: D, C, E
![Page 85: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/85.jpg)
Slide 85
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A
B C √ D √ E √ F √ G √ H
Dequeue F. Visit F. Enqueue unenqueued nodes adjacent to F.
Q à G
Nodes visited: D, C, E, F
![Page 86: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/86.jpg)
Slide 86
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A
B C √ D √ E √ F √ G √ H √
Dequeue G. Visit G. Enqueue unenqueued nodes adjacent to G.
Q à H
Nodes visited: D, C, E, F, G
![Page 87: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/87.jpg)
Slide 87
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A √
B √ C √ D √ E √ F √ G √ H √
Dequeue H. Visit H. Enqueue unenqueued nodes adjacent to H.
Q à A à B
Nodes visited: D, C, E, F, G, H
![Page 88: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/88.jpg)
Slide 88
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A √
B √ C √ D √ E √ F √ G √ H √
Dequeue A. Visit A. Enqueue unenqueued nodes adjacent to A.
Q à B
Nodes visited: D, C, E, F, G, H, A
![Page 89: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/89.jpg)
Slide 89
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A √
B √ C √ D √ E √ F √ G √ H √
Dequeue B. Visit B. Enqueue unenqueued nodes adjacent to B.
Q empty
Nodes visited: D, C, E, F, G, H, A, B
![Page 90: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/90.jpg)
Slide 90
A
H B
F
E
D
C
G
Walk-Through Enqueued Array A √
B √ C √ D √ E √ F √ G √ H √
Q empty. Algorithm done.
Q empty
Nodes visited: D, C, E, F, G, H, A, B
![Page 91: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/91.jpg)
Slide 91
Consider Trees
1. What do we call a breadth-first traversal on trees?
![Page 92: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/92.jpg)
Slide 92
![Page 93: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/93.jpg)
Slide 93
Dijkstra’s Algorithm
Text Read Weiss, § 9.3
Dijkstra’s Algorithm Single Source Multiple Destination Shortest Path Algorithm
![Page 94: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/94.jpg)
Slide 94
Requirements
• Works with directed and undirected graphs • Works with weighted and unweighted graphs • Rare type of algorithm è
A greedy algorithm that produces an optimal solution
![Page 95: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/95.jpg)
Slide 95
25
A
H B
F
E
D
C
G
Walk-Through
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Initialize array K dv pv
A F ∞ -
B F ∞ -
C F ∞ -
D F ∞ -
E F ∞ -
F F ∞ -
G F ∞ -
H F ∞ -
2
![Page 96: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/96.jpg)
Slide 96
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Start with G K dv pv
A B
C D E F G T 0 -
H
2
![Page 97: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/97.jpg)
Slide 97
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Update unselected nodes K dv pv
A B
C D 2 G
E F G T 0 -
H 3 G
2
![Page 98: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/98.jpg)
Slide 98
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Select minimum distance K dv pv
A B
C D T 2 G
E F G T 0 -
H 3 G
2
![Page 99: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/99.jpg)
Slide 99
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Update unselected nodes K dv pv
A B
C D T 2 G
E 27 D
F 20 D
G T 0 -
H 3 G
2
![Page 100: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/100.jpg)
Slide 100
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Select minimum distance K dv pv
A B
C D T 2 G
E 27 D
F 20 D
G T 0 -
H T 3 G
2
![Page 101: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/101.jpg)
Slide 101
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Update unselected nodes K dv pv
A 7 H
B 12 H
C D T 2 G
E 27 D
F 20 D
G T 0 -
H T 3 G
2
![Page 102: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/102.jpg)
Slide 102
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Select minimum distance K dv pv
A T 7 H
B 12 H
C D T 2 G
E 27 D
F 20 D
G T 0 -
H T 3 G
2
![Page 103: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/103.jpg)
Slide 103
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Update unselected nodes K dv pv
A T 7 H
B 12 H
C D T 2 G
E 27 D
F 17 A
G T 0 -
H T 3 G
2
![Page 104: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/104.jpg)
Slide 104
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Select minimum distance K dv pv
A T 7 H
B T 12 H
C D T 2 G
E 27 D
F 17 A
G T 0 -
H T 3 G
2
![Page 105: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/105.jpg)
Slide 105
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Update unselected nodes K dv pv
A T 7 H
B T 12 H
C 16 B
D T 2 G
E 22 B
F 17 A
G T 0 -
H T 3 G
2
![Page 106: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/106.jpg)
Slide 106
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Select minimum distance K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F 17 A
G T 0 -
H T 3 G
2
![Page 107: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/107.jpg)
Slide 107
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Update unselected nodes K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F 17 A
G T 0 -
H T 3 G
2
![Page 108: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/108.jpg)
Slide 108
25
A
H B
F
E
D
C
G
9
7
2
10
18
3 4
3
7
5
8
9 4
3
10
Select minimum distance K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F T 17 A
G T 0 -
H T 3 G
2
![Page 109: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/109.jpg)
Slide 109
25
A
H B
F
E
D
C
G
9
7
2
10
18
2
4
3
7
5
8
9 4
3
10
Update unselected nodes K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 19 F
F T 17 A
G T 0 -
H T 3 G
![Page 110: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/110.jpg)
Slide 110
25
A
H B
F
E
D
C
G
9
7
2
10
18
2
4
3
7
5
8
9 4
3
10
Select minimum distance K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E T 19 F
F T 17 A
G T 0 -
H T 3 G
Done
![Page 111: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/111.jpg)
Slide 111
Dijkstra’s Algorithm
http://www.youtube.com/watch?v=8Ls1RqHCOPw
![Page 112: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/112.jpg)
Slide 112
Order of Complexity
• Analysis – findMin() takes O(V) time – outer loop iterates (V-1) times è O(V2) time
• Optimal for dense graphs, i.e., |E| = O(V2) • Suboptimal for sparse graphs, i.e., |E| = O(V)
![Page 113: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/113.jpg)
Slide 113
Order of Complexity
If the graph is sparse, i.e., |E| = O(V) – maintain distances in a priority
queue – insert new (shorter) distance
produced by line 10 of Figure 9.32
è O(|E| log |V|) complexity
![Page 114: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/114.jpg)
Slide 114
Negative Edge Weights
Read § 9.3.3 Dijkstra’s algorithm as shown in
Figure 9.32 does not work! Why?
![Page 115: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/115.jpg)
Slide 115
Acyclic Graphs
• Read § 9.3.4 • Combine topological sort with Dijkstra’s
algorithm
![Page 116: Introduction to Graphs - Drexel CCIamd435/courses/cs260/lectures/L-7_Graph... · Slide 33 Uses for Graphs • Precedence Constraints: Suppose you have a set of jobs to complete, but](https://reader030.fdocuments.in/reader030/viewer/2022040904/5e76fbb1d2916711b60523c3/html5/thumbnails/116.jpg)
Slide 116
All-Pairs Shortest Paths
• One option: run Dijktra’s algorithm |V| times è O(V3) time
• A more efficient O(V3) time algorithm is discussed in Chapter 10