graphs. 2 some examples and terminology a graph is a set of vertices (nodes) and a set of edges...
TRANSCRIPT
![Page 1: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/1.jpg)
Graphs
![Page 2: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/2.jpg)
2
Some Examples and Terminology
A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly two vertices• Edges can be undirected or directed
(digraph)
A subgraph is a portion of a graph that itself is a graph
![Page 3: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/3.jpg)
3
A portion of a road map treated as a graph
NodesNodes
EdgesEdges
![Page 4: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/4.jpg)
4
A directed graph representing part of a city street map
![Page 5: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/5.jpg)
5
(a) A maze; (b) its representation as a graph
![Page 6: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/6.jpg)
6
The prerequisite structure for a selection of courses as a directed graph without cycles.
![Page 7: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/7.jpg)
7
PathsA sequence of edges that connect two vertices in a graphIn a directed graph the direction of the edges must be considered• Called a directed path
A cycle is a path that begins and ends at same vertex and does not pass through any vertex more than onceA graph with no cycles is acyclic
![Page 8: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/8.jpg)
8
Weights
A weighted graph has values on its edges• Weights, costs, etc.
A path in a weighted graph also has weight or cost• The sum of the edge weights
Examples of weights• Miles between nodes on a map• Driving time between nodes• Taxi cost between node locations
![Page 9: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/9.jpg)
9
A weighted graph
![Page 10: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/10.jpg)
10
Connected Graphs
A connected graph• Has a path between every pair of
distinct vertices
A complete graph• Has an edge between every pair of
distinct vertices
A disconnected graph• Not connected
![Page 11: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/11.jpg)
11
Connected Graphs
Undirected graphs
![Page 12: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/12.jpg)
12
Adjacent Vertices
Two vertices are adjacent in an undirected graph if they are joined by an edge
Sometimes adjacent vertices are called neighbors
Vertex A is adjacent to B, but B is not adjacent to A.
![Page 13: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/13.jpg)
13
Note the graph with two subgraphs • Each subgraph connected• Entire graph disconnected
Airline routes
![Page 14: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/14.jpg)
14
TreesAll trees are graphs• But not all graphs are trees
A tree is a connected graph without cyclesTraversals• Preorder (and, technically, inorder and
postorder) traversals are examples of depth-first traversal
• Level-order traversal of a tree is an example of breadth-first traversal
Visit a node• Process the node’s data and/or mark the node
as visited
![Page 15: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/15.jpg)
15
Trees
Visitation order of two traversals; (a) depth first; (b) breadth first.
![Page 16: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/16.jpg)
16
Depth-First Traversal
Visits a vertex, then• A neighbor of the vertex, • A neighbor of the neighbor,• Etc.
Advance as far as possible from the original vertex
Then back up by one vertex• Considers the next neighbor
![Page 17: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/17.jpg)
17
Algorithm depthFirstTraversal (originVertex)traversalOrder = a new queue for the resulting traversal ordervertexStack = a new stack to hold vertices as they are visitedMark originVertex as visitedtraversalOrder.enqueue (originVertex)vertexStack.push (originVertex)while (!vertexStack.isEmpty ()) {
topVertex = vertexStack.peek ()if (topVertex has an unvisited neighbor) {
nextNeighbor = next unvisited neighbor of topVertexMark nextNeighbor as visitedtraversalOrder.enqueue (nextNeighbor)vertexStack.push (nextNeighbor)
}else // all neighbors are visited
vertexStack.pop ()}return traversalOrder
![Page 18: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/18.jpg)
18
Depth-First Traversal
Trace of a depth-first traversal
beginning at vertex A.
Assumes that children are placed on the stack in alphabetic (or numeric order).
![Page 19: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/19.jpg)
19
Breadth-First TraversalAlgorithm for breadth-first traversal of nonempty graph beginning at a given vertex
Algorithm breadthFirstTraversal(originVertex)vertexQueue = a new queue to hold neighborstraversalOrder = a new queue for the resulting traversal orderMark originVertex as visitedtraversalOrder.enqueue(originVertex)vertexQueue.enqueue(originVertex)while (!vertexQueue.isEmpty()) {
frontVertex = vertexQueue.dequeue()while (frontVertex has an unvisited neighbor) {
nextNeighbor = next unvisited neighbor of frontVertexMark nextNeighbor as visitedtraversalOrder.enqueue(nextNeighbor)vertexQueue.enqueue(nextNeighbor)
}}return traversalOrder
A breadth-first traversal visits a vertex and then each of the
vertex's neighbors before advancing
A breadth-first traversal visits a vertex and then each of the
vertex's neighbors before advancing
![Page 20: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/20.jpg)
20
Breadth-First TraversalA trace of a breadth-first
traversal for a directed graph,
beginning at vertex A.
Assumes that children are placed in the queue in alphabetic (or numeric order).
![Page 21: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/21.jpg)
21
Implementations of the ADT Graph
A directed graph and implementations using adjacency lists and an adjacency matrix.
A B C D
A 0 1 1 1
B 0 0 0 0
C 0 0 0 0
D 1 0 1 0
![Page 22: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/22.jpg)
22
![Page 23: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/23.jpg)
23
Topological Order
Given a directed graph without cycles
In a topological order • Vertex a precedes vertex b whenever
a directed edge exists from a to b
![Page 24: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/24.jpg)
24
Topological Order
Three topological orders for the indicated graph.
![Page 25: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/25.jpg)
25
Topological Order
An impossible prerequisite structure for three courses as a directed graph with a cycle.
![Page 26: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/26.jpg)
26
Topological Order
Algorithm for a topological sort
Algorithm getTopologicalSort()vertexStack = a new stack to hold vertices as they are visitedn = number of vertices in the graphfor (counter = 1 to n) {
nextVertex = an unvisited vertex having no unvisited successorsMark nextVertex as visitedstack.push(nextVertex)
}return stack
![Page 27: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/27.jpg)
27
Topological Sorting
Algorithm getTopologicalSort()vertexStack = a new stack to hold vertices as they are visitedn = number of vertices in the graphfor (counter = 1 to n) {
nextVertex = an unvisited vertex having no unvisited successorsMark nextVertex as visitedstack.push(nextVertex)
}return stack
![Page 28: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/28.jpg)
28
Topological Order
Finding a topological order
![Page 29: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/29.jpg)
29
Shortest Path in an Unweighted Graph
(a) an unweighted graph and (b) the possible paths from vertex A to vertex H.
![Page 30: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/30.jpg)
30
Shortest Path in an Unweighted Graph
The previous graph after the shortest-path algorithm has traversed from vertex A to vertex H
![Page 31: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/31.jpg)
31
Shortest Path in an Unweighted Graph
Finding the shortest path from vertex A to vertex H.
![Page 32: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/32.jpg)
32
Shortest Path in an Weighted Graph
(a) A weighted graph and (b) the possible paths from vertex A to vertex H.
![Page 33: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/33.jpg)
33
Shortest Path in an Weighted Graph
Shortest path between two given vertices• Smallest edge-weight sum
Algorithm based on breadth-first traversal
Several paths in a weighted graph might have same minimum edge-weight sum• Algorithm given by text finds only one of these
paths
![Page 34: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/34.jpg)
34
Shortest Path in an Weighted Graph
Finding the shortest path from vertex A
to vertex H
![Page 35: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/35.jpg)
35
Shortest Path in an Weighted Graph
The previous graph after finding the shortest path from vertex A to vertex H.
![Page 36: Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly](https://reader030.vdocument.in/reader030/viewer/2022032721/56649ccf5503460f9499b521/html5/thumbnails/36.jpg)
36
Java Interfaces for the ADT Graph
Methods in the BasicGraphInterface• addVertex• addEdge• hasEdge• isEmpty• getNumberOfVertices• getNumberOfEdges• clear