Presentation is loading. Please wait.

Presentation is loading. Please wait.

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.

Similar presentations


Presentation on theme: "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10."— Presentation transcript:

1 IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Graphs

2 2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Application Definition Terminology Graph Representation Outline

3 3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Graph Application Networks Maps Shortest path Scheduling (Project Planning)

4 4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Definition A graph G = (V, E) is composed of: V: a set of vertices/nodes E: a set of edges connecting the nodes in V An edge e = (a, b) is a pair of nodes, where a and b  V

5 5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Terminology Undirected graph Directed graph Adjacent nodes: connected by an edge Degree (of a vertex): # of adjacent nodes for directed graph in-degree out-degree

6 6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Weighted Graph Weighted graph: each edge has a weight, or cost V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V V = {V 0, V 1, V 2, V 3, V 4, V 5, V 6 } |V| = 7; |E| = 12

7 7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Path: sequence of nodes v 1, v 2,...v k such that consecutive nodes v i and v i+1 are adjacent. Simple path: no repeated nodes Cycle: simple path, except that the last vertex is the same as the first vertex DAG (Directed Acyclic Graph): directed graph with no cycles. More Terminology V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V

8 8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 More Terminology Connected Graph: any two nodes are connected by some path Subgraph: subset of nodes and edges forming a graph

9 9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Representation

10 10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Representation: Edge List Edge List: store nodes and edges as unsorted sequences. Easy to implement. Finding the edges incident on a given vertex is inefficient since it requires examining the entire edge sequence.

11 11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Adjacency List of a vertex v: sequence of nodes adjacent to v represent the graph by the adjacency lists of all the nodes Representation: Adjacency List

12 12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Matrix M with entries for all pairs of nodes M[i,j] = true means that there is an edge (i,j) in the graph. M[i,j] = false means that there is no edge (i,j) in the graph. There is an entry for every possible edge, therefore: Space = O(N 2 ) Representation: Adjacency Matrix

13 13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Representation: Adjacency Matrix

14 14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Topological Sorting A topological sort of a directed acyclic graph is an ordering on the nodes such that all edges go from left to right. Only an acyclic graph can have a topological sort, because a directed cycle must eventually return home to the source of the cycle. However, every DAG has at least one topological sort, and we can use depth-first search to find such an ordering. Topological sorting proves very useful in scheduling jobs in their proper sequence

15 15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting Start from vertex that have in-degree 0

16 16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

17 17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

18 18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

19 19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

20 20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

21 21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

22 22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V V5V5 Topological Sorting

23 23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

24 24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

25 25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Shortest Path: Unweighted Graph V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V6 V2V2 V6V6 Starting vertex: V 2 Use BFS (Breadth First Search) instead of DFS (Depth First Search) to find shortest path in unweighted graph (or each edge have the same weight)

26 26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 In many applications, e.g., transportation networks, the edges of a graph have different weights. Dijkstra’s algorithm finds the shortest path from a start vertex s to all the other nodes in a graph with non-negative edge weights Dijkstra’s algorithm uses a greedy method Shortest Path: Positive Weighted Graph

27 27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm Computes for each vertex v the distance of v from the start vertex s, that is, the weight of a shortest path between s and v. Keeps track of the set of nodes for which the shortest path has been computed (the white cloud W), the distance has been computed (the grey cloud G), and the distance has not been computed (the black cloud B) Uses a label D[v] to store an approximation of the distance between s and v When a vertex v is added to the cloud, its label D[v] is equal to the actual distance between s and v Initially, the cloud W contains s, and we set D[s] = 0 D[v] =  for v  s

28 28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V      0 D[V 2 ] = 0, others  Add V 2 to “white cloud”

29 29 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Expanding The White Cloud meaning of D[z]: length of shortest path from s to z that uses only intermediate nodes in the white cloud after a new vertex u is added to the cloud, we need to check whether u is a better routing vertex to reach z let u be a vertex not in the white cloud that has smallest label D[u] we add u to the white cloud W we update the labels of the adjacent nodes of u as follows for each vertex z adjacent to u do if z is not in the cloud W then if D[u] + weight(u,z) < D[z] then D[z] = D[u] + weight(u,z)

30 30 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V  2 5   0 After V 2 added to cloud, update D[V x ] where V x is adjacent to V 2

31 31 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V  2 5   0 Add node with minimum total weight (V 3 ) to cloud.

32 32 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V  After V 3 added to the white cloud, update D[V x ] where V x is adjacent to V 3

33 33 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V  Add node with minimum total weight (V 4 ) to cloud.

34 34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V  After V 4 added to the white cloud, update D[V x ] where V x is adjacent to V 4

35 35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V  Add node with minimum total weight (V 0 ) to cloud.

36 36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V After V 0 added to the white cloud, update D[V x ] where V x is adjacent to V 0

37 37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Add node with minimum total weight (V 5 ) to cloud. after V 5 added into the white cloud, update D[V x ] which V x is the adjacent of V 5

38 38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Add node with minimum total weight (V 6 ) to cloud. After V 6 added to the white cloud, update D[V x ] where V x is adjacent to V 6

39 39 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Dijkstra’s Algorithm: Stages V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Add node with minimum total weight (V 1 ) to cloud. After V 1 added to the white cloud, update D[V x ] where V x is adjacent to V 1

40 40 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Other Shortest Path Problems Negative-weighted Shortest-path All-Pair Shortest Path: Floyd

41 41 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Minimum Spanning Tree (MST) A tree formed from graph edges that connects all the nodes at lowest total cost. The cost of a spanning tree is the sum of the costs of the edges in the tree Application: find the least amount of wire necessary to connect a group of homes or cities connect all the computers in a building with the least amount of cable

42 42 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Minimum Spanning Tree (MST)

43 43 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V2V2 V3V3 V4V4 V6V6 V7V V5V5 Minimum Spanning Tree: A Graph

44 44 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V2V2 V3V3 V4V4 V6V V5V5 V7V7 Minimum Spanning Tree

45 45 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Prim’s Algorithm Start from one vertex Grow the rest of the tree one edge at a time. repeatedly selects the smallest weight edge that will enlarge the tree Greedy algorithms: we make the decision of what to do next by selecting the best local option from all available choices without regard to the global structure.

46 46 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

47 47 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

48 48 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

49 49 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

50 50 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

51 51 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

52 52 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

53 53 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

54 54 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

55 55 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Kruskal’s Algorithm add the edges one at a time, by increasing weight accept an edge if it does not create a cycle

56 56 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

57 57 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

58 58 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

59 59 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

60 60 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

61 61 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

62 62 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

63 63 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

64 64 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Summary Graphs are important data structures! Maps, networks, job schedules, etc. Graphs consist of nodes and edges between them. Representation: Edge list Adjacency list Adjacency matrix Popular algorithms Topological sorting Shortest path Minimum spanning tree


Download ppt "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10."

Similar presentations


Ads by Google