graphs - hiram collegecs.hiram.edu/~walkerel/cs201/graph.pdfgraphs and trees! • every tree is a...
TRANSCRIPT
![Page 1: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/1.jpg)
Graphs!
Ellen Walker!CPSC 201 Data Structures!
Hiram College!
![Page 2: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/2.jpg)
Basic Graph Terminology!
A C
D B
E
A C
D B
E
Vertex (node)
Edge (link)
cycle
Directed Graph
No cycle
Undirected Graph V = {A,B,C,D,E} E = {{A,B}, {A,C}, {A,D}, {A,E}, {D, E}}
V = {A,B,C,D,E} E = {(A,B), (A,E), (A,D), (D,E), (A,C).(C,A)}
cycle
Path
![Page 3: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/3.jpg)
More Terminology!
A C
D B
E
• A is adjacent to B, C, D and E (degree is 4)!
• D is adjacent to A and E ( degree of D is 2)!
• There are 2 connected components (ABCDE and FG)!
• Path BAE is a simple path ; Path BABAC is not.!
• Path ADEA is a cycle!
F
G
![Page 4: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/4.jpg)
Graph Application Example: Prerequisites!
CPSC 171
CPSC 172
CPSC 201
CPSC 240
CPSC 400
CPSC 401
CPSC 331 CPSC 361 CPSC 386
CPSC 152
![Page 5: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/5.jpg)
Graph Application Example: Travel Planning!
CLE
ORD
SFO
SJC CLT
PHL DEN
CVG
1
1.5
2
1.5
4
4
2
2.5
2
4
4 4
1
![Page 6: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/6.jpg)
Graphs and Trees!
• Every tree is a graph!– It is connected!– It has no cycles!– Either all links are directed “downward” away from
the root, or it is undirected!• Every connected, no-cycle, undirected graph
can be a tree!– Any node can be chosen as the root!
![Page 7: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/7.jpg)
Graph ADT !!
• A set of methods!– Create a new graph (specify # vertices)!– Iterate through vertices of graph!– Iterate through neighbors of a vertex!– Is there an edge between 2 vertices?!– What is the weight of an edge between 2 vertices?!– Insert an edge into the graph!
• Other options might be available!– E.g. add vertices, connect a vertex to another, etc.!
![Page 8: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/8.jpg)
Representing Vertices and Edges!
• Vertex represented as integer!– Doesnʼt allow named vertices!– Index into an array of names if you need them!
• Edge represented by a class!– Source vertex, destination vertex, weight (default
1.0)!– For undirected graph, add each edge in both
directions!
![Page 9: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/9.jpg)
Graph Representations!
• Adjacency List!– Each vertex is associated with a list of edges!– Looks a lot like a hash table; linked lists hanging
off an array!• Adjacency Matrix!
– 2D matrix: M[R][C] = weight of edge from R to C!
![Page 10: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/10.jpg)
Adjacency List Example (Directed)!
0 2
3 1
4
0!1!2!3!4!
1 2 3 4
0 4
![Page 11: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/11.jpg)
Adjacency List Example (Undirected)!
0 2
3 1
4
0!1!2!3!4!
1 2 3 4
0 0
0
0 4 3
![Page 12: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/12.jpg)
Adjacency Matrix Example!
0! 1! 2! 3! 4!
0! 5! 8! 7! 7!1! x! x! x! x!2! 10! x! x! x!3! x! x! x! 5!
4! x! x! x! x!
0 2
3 1
4
10 8
7
5 7
5
Value at row,col = weight from row to col The value “x” means infinity (Double.POSITIVE_INFINITY in Java) Unweighted graph can use boolean: (edge = true)
![Page 13: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/13.jpg)
Graph Traversals vs. Tree Traversals!
• Tree has no cycles; graph can have cycles!– Mark visited nodes, and donʼt repeat them! !
• Tree is connected; graph does not have to be!– After your traversal is “finished” check to see if there are
leftover (not visited) nodes!• A graph traversal induces a tree structure on the
graph!– The edges that are used to “find” nodes are part of the tree!– The root of the tree is the node that started the traversal!
![Page 14: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/14.jpg)
Breadth-First vs. Depth-First!
• Breadth-first!– Visit all my immediate neighbors before visiting
their neighbors!– In tree - all children before any grandchildren
(level-order traversal)!• Depth-first!
– Visit first child, first grandchild, … as deep as possible before looking at second child
![Page 15: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/15.jpg)
Breadth First Algorithm!
• For each vertex!– If the vertex is unseen, offer it to the queue and
mark it identified!– While the queue is not empty!
• Current_vertex = q.poll();!• For each neighbor of the current vertex!
– If the neighbor is not yet visited, offer it to the queue and mark it identified!
• Mark current vertex as visited!• End for!
– End While!
![Page 16: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/16.jpg)
Three Kinds of Nodes!
• Unseen (white in your textbook)!– Nodes that have not yet been seen at all!
• Identified (pale blue in your textbook)!– Nodes that are currently in the queue!– Must have at least one visited neighbor!
• Visited (dark blue in your textbook)!– Nodes that have been completed!– All neighbors are visited or identified!
![Page 17: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/17.jpg)
Breadth-First Search example!
![Page 18: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/18.jpg)
Depth First Search Algorithm!
• For each vertex!– If the vertex is unseen, push it on the stack and
mark it identified!– While the stack is not empty!
• Current_vertex = q.pop();!• For each neighbor of the current vertex!
– If the neighbor is not yet visited, push it on the stack and mark it identified!
• Mark current vertex as visited!• End for!
– End While!
![Page 19: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/19.jpg)
Depth First Search Example!
![Page 20: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/20.jpg)
Recursive Depth First Search!
• Rec_dfs(current-node){!• mark current-node identified!• for each neighbor of current_node!• if neighbor is not identified!• (set parent of neighbor to current node)!• rec_dfs(neighbor)!• mark current-node visited!• }!
![Page 21: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/21.jpg)
Comments on Recursive DFS!
• Recursive DFS visits the same nodes, in the same order as Stack DFS!
• Recursive DFS actually uses a stack!• (Why?)!• Why is there no recursive BFS?!
![Page 22: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/22.jpg)
DFS / BFS analysis!
• Each search visits every node, and considers every neighbor of every node!– The number of nodes visited is O(V)!– The number of neigbors considered is O(E)!
• Every vertex is a neighbor for one of its edges!– Time proportional to V+E!
• But, since E >> V (there cannot be fewer than O(V) edges, but there can be O(V2 )), overall time is O(E) in both cases!
![Page 23: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/23.jpg)
Common Graph Algorithms!
• Dijkstraʼs algorithm!– Shortest path from one vertex to all others in a
weighted graph!– If the graph isnʼt weighted use BFS. The path you
take to the node is by definition the shortest.!• Primʼs algorithm!
– Finding the spanning tree with the minimum total weight!
– A spanning tree is a subset of (V-1) edges that minimally connects the graph!
![Page 24: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/24.jpg)
Dijkstraʼs Algorithm!
• The main idea:!– If the (best possible known) cost from point A to
point B is x!– And there is a connection from B to C that costs y,!– Then the (best possible known) path from point A
to point C through B costs x+y!
![Page 25: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/25.jpg)
Keeping Track of Paths!
• We will find all possible distances from a starting node, A!
• Create a table of all other nodes (e.g. B,C,D)!• Initially, distances are all infinite
(unreachable)!• Now, weʼll visit one node at a time (starting
with A) and adding all its links to the table…!
![Page 26: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/26.jpg)
Visiting a node!
• Assume you are looking at node A!– Suppose there is an edge from A to B costing k!– Let d(A) and d(B) be the distances (in the table)
from the starting node to A and B respectively!– If d(A)+k < d(B), change d(B) to d(A)+k and mark
Bʼs predecessor as A!• Do the above for all edges of A!
![Page 27: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed](https://reader034.vdocument.in/reader034/viewer/2022042110/5e8b462e7635a47b981ecd9e/html5/thumbnails/27.jpg)
Putting it all together!
• Initialize the table!• Visit the starting node!• While (not all nodes have been visited)!
– Visit the node that is closest to the starting point!• When done, the table will contain all shortest
distances !