Chapter+9+ +graph
Chapter+9+ +graph
A Graph G consists of a set V, whose members are called the vertices of G, together with a set E of pairs of distinct vertices from V. The pairs in E are called the edges of G.
If the pairs are unordered, G is called an undirected graph or a graph. Otherwise, G is called a directed graph or a digraph. Two vertices in an undirected graph are called adjacent if there is an edge from the first to the second.
Chapter 9 - Graph
A path is a sequence of distinct vertices, each adjacent to the next. A cycle is a path containing at least three vertices such that the last vertex on the path is adjacent to the first.
A graph is called connected if there is a path from any vertex to any other vertex. A free tree is defined as a connected undirected graph with no cycles.
Chapter 9 - Graph
In a directed graph a path or a cycle means always moving in the direction indicated by the arrows. A directed graph is called strongly connected if there is a directed path from any vertex to any other vertex.
If we suppress the direction of the edges and the resulting undirected graph is connected, we call the directed graph weakly connected
Examples of Graph
Examples of Graph
Directed graph
Adjacency set
Adjacency table
Digraph count <integer> // Number of vertices edge <array of <array of <boolean> > > // Adjacency table End Digraph
Weighted-graph
vertex vector
adjacency table
Directed graph
contiguous structure
linked structure
mixed structure
Directed graph
first_vertex
linked structure
10
digraph
0 1 2 3
head
head head
1 2
2 3
GraphNode vertex <VertexType> // (key field) adjVertex<LinkedList of< VertexType >> indegree <int> are hidden outdegree <int> from the isMarked <boolean> image below End GraphNode
head
2
GraphNode vertex adjVertex
head
11
mixed list
contiguous list
GraphNode
<void> GraphNode() // constructor of GraphNode 1. indegree = 0 2. outdegree = 0 3. adjVertex.clear() // By default, constructor of adjVertex made it empty. End GraphNode
GraphNode
vertex adjVertex
head
14
15
Digraph
Digraph private: digraph <List of <GraphNode> > // using of List ADT . <void> Remove_EdgesToVertex(val VertexTo <VertexType>) public: <ErrorCode> InsertVertex (val newVertex <VertexType>) <ErrorCode> DeleteVertex (val Vertex <VertexType>) <ErrorCode> InsertEdge (val VertexFrom <VertexType>, val VertexTo <VertexType>) <ErrorCode> DeleteEdge (val VertexFrom <VertexType>, val VertexTo <VertexType>)
// Other methods for Graph Traversal. End Digraph
16
17
18
20
Post
if VertexFrom and VertexTo are in the digraph, and the edge from VertexFrom to VertexTo is not in the digraph, it has been inserted. Return success, overflow, notFound_VertexFrom , notFound_VertexTo or duplicate_error
23
1. 2. 3.
DataFrom.vertex = VertexFrom DataTo.vertex = VertexTo if ( digraph.Retrieve(DataFrom) = success ) 1. if ( digraph.Retrieve(DataTo) = success ) 1. newData = DataFrom 2. if ( newData.adjVertex.Search(VertexTo) = found ) 1. return duplicate_error 3. if ( newData.adjVertex.Insert(VertexTo) = success ) 1. newData.outdegree = newData.outdegree +1 2. digraph.Replace(newData, DataFrom) 3. return success GraphNode 4. else vertex <VertexType> // (key field) 1. return overflow adjVertex<List of< VertexType >> 2. else indegree <int> 1. return notFound_VertexTo outdegree <int> 4. else isMarked <boolean> 1. return notFound_VertexFrom 24 End GraphNode End InsertEdge
Post
if VertexFrom and VertexTo are in the digraph, and the edge from VertexFrom to VertexTo is in the digraph, it has been removed Return success, notFound_VertexFrom , notFound_VertexTo or notFound_Edge
25
1. 2. 3.
DataFrom.vertex = VertexFrom DataTo.vertex = VertexTo if ( digraph.Retrieve(DataFrom) = success ) 1. if ( digraph.Retrieve(DataTo) = success ) 1. newData = DataFrom 2. if ( newData.adjVertex.Remove(VertexTo) = success ) 1. newData.outdegree = newData.outdegree -1 2. digraph.Replace(newData, DataFrom) 3. return success 3. else 1. return notFound_Edge GraphNode 2. else vertex <VertexType> // (key field) 1. return notFound_VertexTo adjVertex<List of< VertexType >> 4. else indegree <int> 1. return notFound_VertexFrom outdegree <int> End DeleteEdge isMarked <boolean> 26 End GraphNode
Graph Traversal
Depth-first traversal: analogous to preorder traversal of an oredered tree.
Breadth-first traversal: analogous to level-by-level traversal of an ordered tree.
27
Depth-first traversal
28
Breadth-first traversal
29
Depth-first traversal
<void> DepthFirst (ref <void> Operation ( ref Data <DataType>))
Traverses the digraph in depth-first order. Post Uses The function Operation has been performed at each vertex of the digraph in depth-first order. Auxiliary function recursiveTraverse to produce the recursive depth-first order.
30
Depth-first traversal
<void> DepthFirst (ref <void> Operation ( ref Data <DataType>))
1. loop (more vertex v in Digraph) 1. unmark (v) 2. loop (more vertex v in Digraph) 1. if (v is unmarked) 1. recursiveTraverse (v, Operation) End DepthFirst
31
Depth-first traversal
<void> recursiveTraverse (ref v <VertexType>, ref <void> Operation ( ref Data <DataType>) )
Traverses the digraph in depth-first order. Pre Post v is a vertex of the digraph. The depth-first traversal, using function Operation, has been completed for v and for all vertices that can be reached from v. Uses function recursiveTraverse recursively.
32
Depth-first traversal
<void> recursiveTraverse(ref v <VertexType>, ref <void> Operation ( ref Data <DataType>) )
1. mark(v) 2. Operation(v) 3. loop (more vertex w adjacent to v) 1. if (vertex w is unmarked) 1. recursiveTraverse (w, Operation) End Traverse
33
Breadth-first traversal
<void> BreadthFirst (ref <void> Operation ( ref Data <DataType>) )
Traverses the digraph in breadth-first order. Post Uses The function Operation has been performed at each vertex of the digraph in breadth-first order. Queue ADT.
34
// BreadthFirst 1. queueObj <Queue> 2. loop (more vertex v in digraph) 1. unmark(v) 3. loop (more vertex v in Digraph) 1. if (vertex v is unmarked) 1. queueObj.EnQueue(v) 2. loop (NOT queueObj .isEmpty()) 1. queueObj.QueueFront(w) 2. queueObj.DeQueue() 3. if (vertex w is unmarked) 1. mark(w) 2. Operation(w) 3. loop (more vertex x adjacent to w) 1. queueObj.EnQueue(x) End BreadthFirst
35
Topological Order
A topological order for G, a directed graph with no cycles, is a sequential listing of all the vertices in G such that, for all vertices v, w G, if there is an edge from v to w, then v precedes w in the sequential listing.
36
Topological Order
37
Topological Order
38
A glossary of technical terms: no term is used in a definition before it is itself defined. The topics in the textbook.
39
Topological Order
<void> DepthTopoSort (ref TopologicalOrder <List>)
Traverses the digraph in depth-first order and made a list of topological order of digraph's vertices. Pre Acyclic digraph. Post The vertices of the digraph are arranged into the list TopologicalOrder with a depth-first traversal of those vertices that do not belong to a cycle. Uses List ADT and function recursiveDepthTopoSort to perform depth-first traversal. Idea: Starts by finding a vertex that has no successors and place it last in the list. Repeatedly add vertices to the beginning of the list. By recursion, places all the successors of a vertex into the topological order. Then, place the vertex itself in a position before any of its successors.
40
Topological Order
41
Topological Order
<void> DepthTopoSort (ref TopologicalOrder <List>)
1. loop (more vertex v in digraph) 1. unmark(v) 2. TopologicalOrder.clear() 3. loop (more vertex v in Digraph) 1. if (vertex v is unmarked) 1. recursiveDepthTopoSort(v, TopologicalOrder) End DepthTopoSort
42
Topological Order
<void> recursiveDepthTopoSort (val v <VertexType>, ref TopologicalOrder <List>)
Pre Post
Uses
Vertex v in digraph does not belong to the partially completed list TopologicalOrder. All the successors of v and finally v itself are added to TopologicalOrder with a depth-first order traversal. List ADT and the function recursiveDepthTopoSort.
Idea: Performs the recursion, based on the outline for the general function traverse. First, places all the successors of v into their positions in the topological order. Then, places v into the order.
43
Topological Order
<void> recursiveDepthTopoSort (val v <VertexType>, ref TopologicalOrder <List>)
1. mark(v) 2. loop (more vertex w adjacent to v) 1. if (vertex w is unmarked) 1. recursiveDepthTopoSort(w, TopologicalOrder) 3. TopologicalOrder.Insert(0, v) End recursiveDepthTopoSort
44
Topological Order
<void> BreadthTopoSort (ref TopologicalOrder <List>)
Traverses the digraph in depth-first order and made a list of topological order of digraph's vertices. Post The vertices of the digraph are arranged into the list TopologicalOrder with a breadth-first traversal of those vertices that do not belong to a cycle. Uses List and Queue ADT. Idea: Starts by finding the vertices that are not successors of any other vertex. Places these vertices into a queue of vertices to be visited. As each vertex is visited, it is removed from the queue and placed in the next available position in the topological order (starting at the beginning). Reduces the indegree of its successors by 1. The vertex having the zero value indegree is ready to processed and is places into the queue. 45
Topological Order
46
Shortest Paths
Given a directed graph in which each edge has a nonnegative weight. Find a path of least total weight from a given vertex, called the source, to every other vertex in the graph.
48
Dijkstra's algorithm
Let tree is the subgraph contains the shotest paths from the source vertex to all other vertices.
At first, add the source vertex to the tree.
S: Set of vertices whose closest distances to the source are known. Add one vertex to S at each stage.
For each vertex v, maintain the distance from the source to v, along a path all of whose vertices are in S, except possibly the last one.
To determine what vertex to add to S at each step, apply the greedy criterion of choosing the vertex v with the smallest distance.
Add v to S.
Update distance from the source for all w not in S, if the path through v and then directly to w is shorter than the previously recorded distance to w.
50
Dijkstra's algorithm
51
Dijkstra's algorithm
52
Dijkstra's algorithm
53
Dijkstra's algorithm
54
Dijkstra's algorithm
55
Dijkstra's algorithm
56
Dijkstra's algorithm
<void> ShortestPath (val source <VertexType>, ref listOfShortestPath <List of <DistanceNode>>)
Finds the shortest paths from source to all other vertices in digraph.
Post
Each node in listOfShortestPath gives the minimal path weight from vertex source to vertex destination in distance field.
// ShortestPath 1. listOfShortestPath.clear() 2. Add source to set S 3. loop (more vertex v in digraph) // Initiate all distances from source to v 1. distanceNode.destination = v 2. distanceNode.distance = weight of edge(source, v) // = infinity if
// edge(source,v) isn't in digraph.
3. listOfShortestPath.Insert(distanceNode) 4. loop (more vertex not in S) // Add one vertex v to S on each step. 1. minWeight = infinity // Choose vertex v with smallest distance. 2. loop (more vertex w not in S) 1. Find the distance x from source to w in listOfShortestPath 2. if (x < minWeight) DistanceNode destination <VertexType> 1. v = w distance <int> 2. minWeight = x 58 End DistanceNode 3. Add v to S.
// ShortestPath (continue) 4. loop (more vertex w not in S) // Update distances from source // to all w not in S 1. Find the distance x from source to w in listOfShortestPath 2. if ( (minWeight + weight of edge from v to w) < x ) 1. Update distance from source to w in listOfShortestPath to (minWeight + weight of edge from v to w) End ShortestPath
Select the adjacent vertex having minimum path to the source vertex
60
61
Spanning Trees
63
Prim's algorithm
Let tree is the minimum spanning tree.
At first, add one vertex to the tree.
64
To determine what vertex to add to S at each step, apply the greedy criterion of choosing the vertex v with the smallest distance from S. Add v to S.
Update distances from S to all vertices v not in S if they are smaller than the previously recorded distances.
65
Prim's algorithm
Select the adjacent vertex having minimum edge to the vertices 66 already in the tree.
Prim's algorithm
<void> MinimumSpanningTree (val source <VertexType>, ref tree <Graph>)
Finds the minimum spanning tree of a connected component of the original graph that contains vertex source.
Post
tree is the minimum spanning tree of a connected component of the original graph that contains vertex source.
1. 2. 3. 4. 5. 6.
tree.clear() tree.InsertVertex(source) Add source to set S listOfDistanceNode.clear() distanceNode.vertexFrom = source loop (more vertex v in graph)//Initiate all distances from source to v 1. distanceNode.vertexTo = v 2. distanceNode.distance = weight of edge(source, v) // = infinity if // edge(source,v) isn't in graph. 3. listOfDistanceNode.Insert(distanceNode)
DistanceNode vertexFrom <VertexType> vertexTo <VertexType> distance <WeightType> 68 End DistanceNode
7. continue = TRUE 8. loop (more vertex not in S) and (continue) //Add one vertex to S on // each step 1. minWeight = infinity //Choose vertex v with smallest distance toS 2. loop (more vertex w not in S) 1. Find the node in listOfDistanceNode with vertexTo is w 2. if (node.distance < minWeight) 1. v = w 2. minWeight = node.distance
3. if (minWeight < infinity) 1. Add v to S. 2. tree.InsertVertex(v) 3. tree.InsertEdge(v,w) 4. loop (more vertex w not in S) // Update distances from v to // all w not in S if they are smaller than the // previously recorded distances in listOfDistanceNode 1. Find the node in listOfDistanceNode with vertexTo is w 2. if ( node.distance > weight of edge(v,w) ) 1. node.vertexFrom = v 2. node.distance = weight of edge(v,w) ) 3. Replace this node with its old node in listOfDistance DistanceNode 4. else vertexFrom <VertexType> 1. continue = FALSE vertexTo <VertexType> End MinimumSpanningTree
distance <WeightType> End DistanceNode
70
Maximum flows
71
Maximum flows
72
Maximum flows
73
Maximum flows
74
Maximum flows
75
Matching
76
Matching
77
Matching
78
Matching
79
Matching
80
Graph coloring
81
Graph coloring
82
Graph coloring
83
Graph coloring
84
Graph coloring
85