Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.

Similar presentations


Presentation on theme: "Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used."— Presentation transcript:

1 Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used in the Priority Queue project of Chapter 11. The lecture includes the algorithms for adding to a heap (including reheapification upward), removing the top of a heap (including reheapification downward), and implementing a heap in a partially-filled array. Prior to this lecture, the students need a good understanding of complete binary trees. It would also help if they have seen binary search trees and the priority queue class. Data Structures and Other Objects Using C++

2 Graph Definitions Graph
A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

3 Graph Definitions Graph
- a nonlinear data structure consisting of nodes and links between nodes. - unlike trees, graph nodes can be linked in any pattern - graphs can be empty. A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

4 Graph Definitions Undirected Graphs
- a finite set of vertices(nodes) together with a finite set of edges/arcs(links between nodes) v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 - edges have no directions - both vertices and edges could have labels A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

5 Graph Definitions Undirected State Graphs
Example: Graph for game states Three Coins in a line H T Rules: 1. You may flip the middle coin anytime. A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... 2. You may flip one of the end coins only if the other two coins are the same as each other.

6 Graph Definitions Undirected State Graphs H T H H T H T T H T H H T T
A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... T H H T T

7 Graph Definitions Directed Graphs
- a finite set of vertices(nodes) together with a finite set of directed edges/arcs(links between nodes) v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 - edges have directions (source  target ) A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... - both vertices and edges could have labels

8 Graph Definitions More Graph Terminology - Loop:
- an edge that connects a vertex to itself v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 v0 - Path: v2 v1 - a sequence of vertices p1,p2,…,pm such that each adjacent pair of pi and pi+1 are connected by an edge. v4 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

9 Graph Definitions More Graph Terminology - Cycle:
- a path p1,p2,…,pm where p1=pm v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 v0 - Multiple Edges: v2 - two or more edges that connect the same two vertices (in the same direction) v1 v4 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

10 Graph Definitions More Graph Terminology - Simple Graph:
- a graph with no loops and no multiple edges v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

11 Graph Implementations
Representing Graphs with an Adjacency Matrix Adjacency Matrix for Directed Graphs: v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 - A square grid of boolean values or weights on the edges - Cell [i,j]==true indicates that there is an edge from vertex i to vertex j. F T v0 v1 v2 v3 v4 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... - Cell [i,j]==w indicates that there is an edge from vertex i to vertex j with weight w

12 Graph Implementations
Representing Graphs with Adjacency Lists Adjacency List for Directed Graphs: v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 - An array of n different linked lists (n = the number of vertices) - each array cell represents a vertex in the graph NULL 5 v1 v3 v2 v4 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... - each array cell [i] has a pointer to a linked list of all vertices directly connected from i

13 Graph Implementations
Representing Graphs with Edge Sets Edge Set for Directed Graphs: v0 v3 v4 v2 v1 e0 e1 e2 e3 e4 e5 - An array of n C++ STL sets (n = the number of vertices) - each array cell represents a vertex in the graph - each array cell [i] has a set object set<dataType> of all vertices directly connected from i A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... 5 1,4 4 2,3

14 Graph Implementations
Graph Class with Weighted Vertices template <class Item> class graph { public: … private: bool edges[MAX][MAX] ; Item labels[MAX] ; std::size_t many_vertices; } static const std::size_t MAX=20; 3.14 0.2 1.75 5.98 2.2 e0 e1 e2 e3 e4 e5 F T v0 v1 v2 v3 v4 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

15 Graph Implementations
Public Functions for the Graph Class - add_vertex(const &Item label) - add_edge(size_t source, size_t target) - remove_edge(size_t source, size_t target) - operator [ ] (size_t vertex) - size( ) - is_edge(size_t source, size_t target) A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... - set<size_t> neighbors(size_t vertex) const

16 Graph Implementations
constructor operator [ ] - creates a graph object with no vertices and no edges - returns a reference to the label of the specified vertex template <class Item> Item& graph<Item>::operator [ ] (std::size_t vertex) { assert(vertex < size()) ; return labels[vertex] ; } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... graph<double> g; … ; g[2] = 7.67;

17 Graph Implementations
add_vertex template <class Item> void graph<Item>::add_vertex (const Item& label) { std::size_t new_vertex_number; std::size_t vertex ; assert(size( ) < MAX ) ; new_vertex_number = many_vertices ; ++many_vertices ; for (vertex=0;vertex<many_vertices;++vertex) { edges[vertex][new_vertex_number] = false ; edges[new_vertex_number][vertex] = false ; } labels[new_vertex_number] = label ; A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

18 Graph Implementations
add_edge template <class Item> void graph<Item>::add_edge (std::size_t source, std::size_t target ) { assert(source < size( )) ; assert(target < size( )) ; edges[source][target] = true; } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

19 Graph Implementations
is_edge template <class Item> bool graph<Item>::is_edge (std::size_t source, std::size_t target ) const { assert(source < size( )) ; assert(target < size( )) ; return edges[source][target]; } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

20 Graph Implementations
neighbors template <class Item> std::set<std::size_t> graph<Item>::neighbors (std::size_t vertex) const { std::set<std::size> answer ; std::size_t i; assert(vertex < size( )) ; for (i=0; i<size( ) ; ++i) { if (edges[vertex][i]) answer.insert(i) ; } return answer ; A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

21 Graph Implementations
remove_edge template <class Item> void graph<Item>::remove_edge(std::size_t source, std::size_t target) { assert(source < size( )) ; assert(target < size( )) ; edges[source][target] = false ; } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

22 Graph Traversals Graph Traversals
- different from tree traversals since there might be cycles in a graph need to mark each vertex as it is processed in order to avoid repetitive visits there are two common ways: depth-first search A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... breadth-first search

23 Graph Traversals Depth-First Search
search proceeds from the start vertex to one of its neighbors, then to one of the neighbor’s neighbors, and so on. search has to back up if it cannot proceed further could be implemented using a stack or recursively A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

24 Graph Traversals Depth-First Search : Example v0 v6 v5 v4 v3 v2 v1 v0
A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

25 Graph Traversals Depth-First Search : Example - continued
Has to back up v6 v5 v4 v3 v2 v1 v0 v6 v5 v4 v3 v2 v1 v0 Has to back up Has to back up v6 v5 v4 v3 v2 v1 v0 v6 v5 v4 v3 v2 v1 v0 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

26 Graph Traversals Depth-First Search : Example - continued 1 2 6 5 3 4
Has to back up 1 v6 v5 v4 v3 v2 v1 v0 v6 v5 v4 v3 v2 v1 v0 2 6 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... 5 3 4

27 Graph Traversals Breadth-First Search
search proceeds from the start vertex to each of its neighbors - after processing all neighbors, search proceeds to the neighbors’ neighbors, and so on. search ends when all reachable vertices have been processed A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... could be implemented using a queue

28 Graph Traversals Breadth-First Search : Example v6 v5 v4 v3 v2 v1 v0
1st level neighbors of v0 v6 v5 v3 v4 v2 v1 v0 v6 v5 v3 v4 v2 v1 v0 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

29 Graph Traversals Breadth-First Search : Example - continued 1 2 3 5 6
2nd level neighbor of v0 v6 v5 v3 v4 v2 v1 v0 v4 v5 v6 v3 v2 v1 v0 3rd level neighbors of v0 1 v6 v5 v4 v3 v2 v1 v0 v6 v5 v4 v3 v2 v1 v0 2 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... 3 5 6 4

30 Graph Traversals Depth-First Search : Implementation
we will implement a recursive version of DFS depth_first(processFunction, aGraph, startingVertex) it will use a boolean array marked[aGraph.MAX] to keep track of the visited vertices depth_first( …) will call a recursive function rec_dfs to actually carry out the search A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

31 Graph Traversals Pseudocode for depth_first( …)
1. Check that the start vertex is a valid vertex number of the graph. 2. Set all the components of marked[ ] to false. 3. Call a separate recursive function rec_dfs to actually carry out the search A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

32 Graph Traversals C++ Code for depth_first
template <class Process, class Item, class SizeType> void depth_first (Process f, graph<Item>& g, SizeType start) { bool marked[g.MAX] ; assert( start < g.size( )) ; std::fill_n(marked, g.size( ), false) ; rec_dfs(f, g, start, marked) ; } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

33 Graph Traversals C++ Code for rec_dfs
template <class Process, class Item, class SizeType> void rec_dfs(Process f, graph<Item>& g, SizeType v,bool marked[ ]) { std::set<std::size_t> connections = g.neighbors(v) ; std::set<std::size_t>::iterator it ; marked[v] = true ; f(g[v]) ; for (it=connections.begin() ; it != connections.end() ; ++it) { if (!marked[*it]) rec_dfs(f, g, *it, marked) ; } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

34 Graph Traversals Breadth-First Search : Implementation
a queue of vertex numbers will be used breadth_first(processFunction, aGraph, startingVertex) it will use a boolean array marked[aGraph.MAX] to keep track of the visited vertices A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

35 Graph Traversals Pseudocode for breadth_first( …)
1. Check that the start vertex is a valid vertex number of the graph. 2. Set all the components of marked[ ] to false. 3. Process the start vertex, mark it, and place it in the queue. 4. Repeat the following steps until the queue is empty: 4.1. Remove a vertex, v, from the queue. A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... 4.2. For each unmarked neighbor u of v do: Process u, mark u, and then place u in the queue.

36 Graph Traversals C++ Code for breadth_first
template <class Process, class Item, class SizeType> void breadth_first (Process f, graph<Item>& g, SizeType start) { bool marked[g.MAX] ; std::set<std::size_t> connections ; std::set<std::size_t>::iterator it ; std::queue<std::size_t> vertex_queue ; assert( start < g.size( )) ; std::fill_n(marked, g.size( ), false) ; /* MAIN PROCESS in the next slide*/ } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

37 Graph Traversals C++ Code for breadth_first - MAIN PROCESS
marked[start] = true ; f(g[start]) ; vertex_queue.push(start) ; do { connections = g.neighbors(vertex_queue.front() ) ; vertex_queue.pop() ; for (it=connections.begin() ; it!=connections.end() ; ++it) { if (!marked[*it]) { marked[*it] = true; f(g[*it]) ; vertex_queue.push(*it) ; } } while (!vertex_queue.empty() ) ; A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

38 Path Algorithms Determining Whether a Path Exists
either DFS or BFS could be used to determine whether a path exists between two vertices u and v 1. Use u as the start vertex of the search and proceed with a DFS or BFS 2. If v is ever visited, then there is a path between u and v. A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... 3. If v is never visited, then there is no path between u and v.

39 Path Algorithms Graphs with Weighted Edges
the edges of a graph might have weights representing “costs”, “distances”, etc. v3 v2 v1 v0 1 6 3 7 2 the weight of a path is the total sum of the weights of all edges in the path the shortest path between u and v is a path between u and v with the smallest weight A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

40 Path Algorithms Shortest Distance Algorithm
Given a vertex start of a graph g, find the shortest distance to each vertex v of the graph. v3 v2 v1 v0 1 6 3 7 2  distance[v] = the weight of the shortest path from start to v. A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... distance [0] [1] [2] [3] 4 6 1

41 Path Algorithms Dijkstra’s Algorithm: Single-Source Shortest Path
Let v0 be the start vertex.  | allowedVertexSet | = 0 v0 9 2 6 distance [0] [1] [2] [3] [5] [4] v1 v5 15 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... v3 8 3 3 1 v2 v4 allowedVertexSet= { } 7

42 Path Algorithms Dijkstra’s Algorithm: - Continued   2 9
| allowedVertexSet | = | {0} | = 1 v0 9 2 6 distance [0] [1] [2] [3] [5] [4] 2 9 v1 v5 15 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... v3 8 3 3 1 allowedVertexSet= { 0 } v2 v4 7

43 Path Algorithms Dijkstra’s Algorithm: - Continued  2 9   10 17 8
| allowedVertexSet | = | {0,1} | = 2 v0 9 2 2 distance [0] [1] [2] [3] 9 [5] [4] allowedVertexSet= 6 10 17 8 v1 v1 v5 15 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... v3 8 3 3 1 { 0, 1 } v2 v4 7

44 Path Algorithms Dijkstra’s Algorithm: - Continued  2 9    10 17 11
| allowedVertexSet | = | {0,1,5} | = 3 v0 9 2 2 distance [0] [1] [2] [3] 9 [5] [4] allowedVertexSet= 6 10 17 11 8 v1 v1 v5 v5 15 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... v3 8 3 1 3 { 0, 1, 5 } v2 v4 7

45 Path Algorithms Dijkstra’s Algorithm: - Continued  2 9     10 11
| allowedVertexSet | = | {0,1,5,2} | = 3 v0 9 2 2 distance [0] [1] [2] [3] 9 [5] [4] allowedVertexSet= 6 10 11 17 11 8 v1 v1 v5 v5 15 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... v3 8 3 3 1 { 0, 1, 5, 2 } v2 v2 v4 7

46 Path Algorithms Dijkstra’s Algorithm: - Continued  2 9      10
| allowedVertexSet | = | {0,1,5,2,3} | = 4 v0 9 2 2 distance [0] [1] [2] [3] 9 [5] [4] allowedVertexSet= 6 10 11 17 11 8 v1 v1 v5 v5 15 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... v3 v3 8 3 3 1 { 0, 1, 5, 2, 3 } v2 v2 v4 7

47 Path Algorithms Dijkstra’s Algorithm: - Continued  2 9      10
| allowedVertexSet | = | {0,1,5,2,3} | = 4 v0 9 2 2 distance [0] [1] [2] [3] 9 [5] [4] allowedVertexSet= 6 10 17 11 11 8 v1 v1 v5 v5 15 A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... v3 v3 8 3 3 1 { 0, 1, 5, 2, 3 } v2 v2 v4 v4 7

48 Path Algorithms Dijkstra’s Algorithm - Pseudocode
Input: a directed graph (of size n) with positive int weights start vertex number Output: distance[ ] array filled with the shortest distances from start to every node in the graph Variables: distance[ ] – int array of size n allowed_vertices – a set of allowed vertices A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

49 Path Algorithms Dijkstra’s Algorithm - Pseudocode
Step 1: Set distance[i]= for all vertex number i except start distance[start] = 0 Step 2: allowed_vertices ={} ; Step 3: for(allowed_size=1; allowed_size<n; ++allowed_size) { Step 3a ; A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... Step 3b ; Step 3c ; }

50 Path Algorithms Dijkstra’s Algorithm – Pseudocode Continued
Step 3a: next = an index of the distance array such that next is not in the allowed_vertices and distance[next] is the minimum among all tentative distances Step 3b: add next to allowed_vertices A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

51 Path Algorithms Dijkstra’s Algorithm – Pseudocode Continued
Step 3c: for(v=0; v<n; ++n) if ((v is not in allowed_vertices) and (there is an edge from next to v)) { sum =distance[next] + weight(next,v) ; A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... if ( sum < distance[v] ) distance[v] = sum ; }

52 Path Algorithms Shortest Path – Pseudocode Continued
- need to keep track of which vertex was the next vertex when the distance[v] was updated  if ( sum < distance[v] ) { distance[v] = sum ; predecessor[v] = next ; } A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

53 Path Algorithms Shortest Path – Printing in reverse order
- the following code will print out the shortest path from the start vertex to v in reverse order  vertex = v ; cout << vertex << endl; while (vertex != start) { vertex = predecessor[vertex] ; A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order... cout << vertex << endl ; }


Download ppt "Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used."

Similar presentations


Ads by Google