10/13/2015IT 328, review graph algorithms1 Topological Sort ( topological order ) Let G = (V, E) be...

21
03/22/22 IT 328, review graph algorithms 1 Sort (topological order) Let G = (V, E) be a directed graph with |V| = n. 1.{ v 1 , v 2 ,.... v n } = V, and 2.for every i and j with 1 i < j n, (v j , v i ) E. 1 7 6 5 0 3 4 2 A Topological Sort is a sequence v 1 , v 2 ,.... v n , such that 1 7 6 5 0 3 4 2 There are more than one such order in this graph!!

Transcript of 10/13/2015IT 328, review graph algorithms1 Topological Sort ( topological order ) Let G = (V, E) be...

04/19/23 IT 328, review graph algorithms 1

Topological Sort (topological order)

Let G = (V, E) be a directed graph with |V| = n.

1. { v1, v2,.... vn } = V, and2. for every i and j with 1 i < j n, (vj , vi) E.

17 6 50 3 42

A Topological Sort is a sequence v1, v2,.... vn, such that

1

7

6

5

0

3

4

2There are more than one such order in this graph!!

04/19/23 IT 328, review graph algorithms 2

If the graph is not acyclic, then there is no topological sort for the graph.

2 4

3 5

7

1

6

9

0

8

Not an acyclic Graph

No way to arrange vertices in this cycle without pointing backwards

04/19/23 IT 328, review graph algorithms 3

Algorithm for finding Topological Sort

Principle: vertex with in-degree 0 should be listed first.

Algorithm:

Repeat the following steps until no more vertex left:

1. Find a vertex v with in-degree 0;

Input G=(V,E)

2. Remove v from V and update E3. Add v to the end of the sort

If can’t find such v and V is not empty then stop the algorithm; this graph is not acyclic

3 5 2 7

1

3

5

04/19/23 IT 328, review graph algorithms 4

Topological Sort (topological order) of an acyclic graph

17 6 50 3 42

1

7

6

0

3

4

52

04/19/23 IT 328, review graph algorithms 5

2 4

3 5

7

1

6

9

0

8

the graph is not acyclic

Fail to find a topological sort (topological order)

0 2 8

04/19/23 IT 328, review graph algorithms 6

2 4

3 5

7

1

6

9

0

8

This graph is acyclic

Topological Sort (topological order) of an acyclic graph

0 2 8 5 4 3 7 9 6 1

04/19/23 IT 328, review graph algorithms 7

Topological Sort in C++// return an array where the first entry is the size of g// or 0 if g is not acyclic;int * graph::topsort(const Graph & g) { int *topo; Graph tempG = g;

topo = new int[g.size()+1];topo[0] = g.size();for (int i=1; i<=topo[0]; i++) {

int v = findVertexwithZeroIn(tempG);if (v == -1) { // g is not acyclic

delete [] topo;topo = new int[1];topo[0] = 0; return topo;

}topo[i] = v;remove_v(tempG, v);

}return topo;

}

04/19/23 IT 328, review graph algorithms 8

Edsger Wybe Dijkstra 1930-2002

Dijkstra Algorithm:

To find the shortest path between two vertices in a weighted graph.

• Algorithm design • Programming languages • Operating systems • Distributed processing • Formal specification and verification

1972 Turing Award

04/19/23 IT 328, review graph algorithms 9

1. Back Tracking....2. Greedy Algorithms: Algorithms that make decisions

based on the current information; and once a decision is made, the decision will not be revised

3. Divide and Conquer....4. Dynamic Programming.....

Common Algorithm Techniques

Problem: Minimized the number of coins for change.

In real life, we can use a greedy algorithm to obtain the minimum number of coins. But in general, we need dynamic programming to do the job

04/19/23 IT 328, review graph algorithms 10

Greedy Algorithms Dynamic Programming

Problem: Minimized the number of coins for 66¢

{ 25¢, 10¢, 5¢, 1¢ } { 25¢, 12¢, 5¢, 1¢ }

25¢ 2 50¢

10¢ 1 10¢

5¢ 1 5¢

1¢ 1 1¢

5 66¢

25¢ 2 50¢

12¢ 1 12¢

5¢ 0 0¢

1¢ 4 4¢

7 66¢

25¢ 2 50¢

12¢ 0 0¢

5¢ 3 15¢

1¢ 1 1¢

6 66¢

Greedy method Greedy method Dynamic method

16¢

16¢

04/19/23 IT 328, review graph algorithms 11

Unweighted Graphs Weighted Graphs

Starting Vertex

wFinal Vertex

Starting Vertex

w Final Vertex

1

52

1 5

9

7

1

Both are using thegreedy algorithm.

0

1

22

2

1

0

2 52 3

3 73

3

12

7

1088

Finding the shortest path between two vertices

04/19/23 IT 328, review graph algorithms 12

Shortest paths of unweighted graphs

PathMap graph::UnweightedShortestPath(Graph & g, int s) {

PathMap SPMap; // create a bookkeeper; for (Graph::iterator itr = g.begin(); itr != g.end(); itr++) {

SPMap[itr->first] = make_pair(inft,-1); // -1 mean no previous } SPMap[s] = make_pair(0,-1); // s is not done yet. deque<int> candidates; candidates.push_back(s); while (!candidates.empty()) {

int v = candidates[0];candidates.pop_front();AdjacencyList ADJ = g[v];for (AdjacencyList::iterator w=ADJ.begin(); w != ADJ.end(); w++) { if (SPMap[w->first].first == inft) {

SPMap[w->first].first = SPMap[v].first+1; SPMap[w->first].second=v;

candidates.push_back(w->first); } // end enqueue next v} // end ADJ interation; all nextvs's next into the queue.

} return SPMap;}

// <vertex, <distance, previous vertex in the path>>typedef map<int,pair<double, int> > PathMap;

04/19/23 IT 328, review graph algorithms 13

Construct a shortest path Map for weighted graph

1 2

3 4

Starting Vertex

5

n Final Vertex

1

52

1 5

9

7

1

0

Starting Vertex

Final Vertex

0,-1

5,0

7,13,1

10,2

2,0

w-1,y

w,x

: decided

3,1

8,4

04/19/23 IT 328, review graph algorithms 14

Shortest paths of weighted graphs (I)

// A Dijkstra's algorithm PathMap graph::ShortestPath(Graph & g, int s) {

PathMap SPMap;

map<int,pair<double, bool> > dist_map; // A distance map // for book keeping; for (Graph::iterator itr = g.begin(); itr != g.end(); itr++) {

dist_map[itr->first] = make_pair(inft,false);SPMap[itr->first] = make_pair(inft,-1);

} dist_map[s] = make_pair(0,false); // s is not done yet. multimap<double,int> candidates ; // next possible vertices //sorted by their distance. candidates.insert(make_pair(0,s)); // start from s;..........}

04/19/23 IT 328, review graph algorithms 15

Shortest paths (II)

// A Dijkstra's algorithm PathMap graph::ShortestPath(Graph & g, int s) { ...... while (! candidates.empty()) {

int v = candidates.begin()->second;double costs2v = candidates.begin()->first;candidates.erase(candidates.begin());if (dist_map[v].second) continue; // v is done after pair

// (weight v) is inserted;dist_map[v] = make_pair(costs2v,true);AdjacencyList ADJ = g[v]; // all not done adjacent vertices

// should be candidates for (AdjacencyList::iterator itr=ADJ.begin(); itr != ADJ.end(); itr++) {

int w = itr->first; if (dist_map[w].second) continue; // this w is done; double cost_via_v = costs2v + itr->second; if (cost_via_v < dist_map[w].first) { dist_map[w] = make_pair(cost_via_v ,false); SPMap[w] = make_pair(cost_via_v,v); } candidates.insert(make_pair(cost_via_v,w));} // end for ADJ iteration;

} // end while !candidates.empty() return SPMap; }

04/19/23 IT 328, review graph algorithms 16

Minimum Spanning Tree of an undirected graph:

The lowest cost to connect all vertices

Since a cycle is not necessary, such a connection must be a tree.

A spanning is a walk such that every vertex is included

11 2

3 4 5

6 71

4

2

2

6

4 3 10

7

85

04/19/23 IT 328, review graph algorithms 17

Prim’s algorithm: grow the minimum spanning tree from a root

11 2

3 4 5

6 71

4

2

2

6

4 3 10

7

85

11 2

3 4 5

6 71

4

2

2

6

04/19/23 IT 328, review graph algorithms 18

Prim’s algorithm: grow the minimum spanning tree from a different root

11 2

3 4 5

6 71

4

2

2

6

4 3 10

7

85

11 2

3 4 5

6 71

4

2

2

6

04/19/23 IT 328, review graph algorithms 19

Kruskal’s Algorithm: construct the minimum spanning from edges

11 2

3 4 5

6 71

4

2

2

6

4 3 10

7

85

11 2

3 4 5

6 71

4

2

2

6

04/19/23 IT 328, review graph algorithms 20

A Hamiltonian cycle is a cycle that contains every vertex;A graph that contains a Hamiltonian cycle is called Hamiltonian

1

2

9

3

510

4

6

7

8

11

12

14

16

15 13

04/19/23 IT 328, review graph algorithms 21

A non-Hamiltonian graph

1

2

3

4

5

6

7

8

9