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

Slides:



Advertisements
Similar presentations
Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Advertisements

Lecture 15. Graph Algorithms
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Minimum Spanning Tree Algorithms
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Minimum Spanning Trees
Greedy Technique.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Topological Sort (topological order)
Short paths and spanning trees
Graph Algorithm.
Minimum Spanning Trees
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Minimum Spanning Trees
Graphs.
Graph Operations And Representation
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
ITEC 2620M Introduction to Data Structures
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPHS.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

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 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Application Definition Terminology Graph Representation Outline

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

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 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 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 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 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 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Representation

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 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 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 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Representation: Adjacency Matrix

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 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 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 V6V Topological Sorting

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

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

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

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

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

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

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

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

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May 2007 Minimum Spanning Tree (MST)

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

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

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 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Prim’s Algorithm

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

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

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

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

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

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

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

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

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 Ruli Manurung (Fasilkom UI)IKI10100: Lecture10 th May V1V1 V3V3 V4V4 V6V6 V7V V5V5 V2V2 Kruskal’s Algorithm

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

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

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

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

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

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

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

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