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.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Graphs and Finding your way in the wilderness
Chapter 9: Graphs Shortest Paths
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Applied Discrete Mathematics Week 12: Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
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.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
SPANNING TREES Lecture 21 CS2110 – Spring
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
CS223 Advanced Data Structures and Algorithms 1 Review for Final Neil Tang 04/27/2010.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Upon completion you will be able to:
Graph Theory. undirected graph node: a, b, c, d, e, f edge: (a, b), (a, c), (b, c), (b, e), (c, d), (c, f), (d, e), (d, f), (e, f) subgraph.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
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.
Data Structures 13th Week
Minimum Spanning Tree Chapter 13.6.
Parallel Graph Algorithms
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Topological Sort (topological order)
Shortest Path Graph represents highway system Edges have weights
Short paths and spanning trees
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Graphs.
Graph Operations And Representation
Minimum-Cost Spanning Tree
Shortest Path Algorithms
Floyd’s Algorithm (shortest-path problem)
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
Weighted Graphs & Shortest Paths
Graphs G = (V, E) V are the vertices; E are the edges.
Graphs.
Parallel Graph Algorithms
Prim’s algorithm for minimum spanning trees
Chapter 9: Graphs Shortest Paths
Review for Final Neil Tang 05/01/2008
Chapter 9 Graph algorithms
Directed Graphs (Part II)
Chapter 9: Graphs Spanning Trees
Minimum-Cost Spanning Tree
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:

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 and 3 edges

Basic Concepts (cont.) n A vertex (v1) is said to be adjacent to (v2) if and only if they share a common edge (e1). v1v3v2 e1e2

Basic Concepts (cont.) n A directed graph or digraph is a graph where direction matters. n Movement in the graph is determined by the direction of the edge. v1 v5 v3 v6 v2 v4

Basic Concepts (cont.) n A path in a graph is a sequence of vertices (v1, v2, v3,…,v n ). n The path length is determined by how many edges are on the path. v1 v5 v3 v6 v2 v

Adjacency List Representation v1 v5 v3 v6 v2 v4 v1v2 v2v3 v6 v3v4 v6 v4v3v5 v5v6 v6v1 Bi-Directional paths are allowed

More terms… weightcost n An edge may have a component known as a weight or cost associated to it. v1v3v2 46

Adjacency Matrix Representation v1 v5 v3 v6 v2 v v1v3v4v2 v1 v5v6 v5 v4 v3 v2 v

Graph Algorithms n Shortest-Path Algorithms Unweighted Shortest Paths Floyd’s Algorithm Dijkstra’s Algorithm All-Pairs Shortest Path n Network Flow Problems Maximum-Flow Algorithm n Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm

Shortest Path Algorithms n Unweighted Shortest Paths ss –Starting with a vertex s, we find the shortest path from s to all the other vertices v1 v7v6 v5 v2 v4 v3 s

Shortest Path Algorithms n Floyd’s Algorithm –Suppose we have the following adjaceny matrix v1v3v4v2 v1 v4 v3 v v1 v3 v2 v

v1v3v4v2 v1 v4 v3 v Matrix Addition We first look at row and column v1 Then we look at each square v’ not in row or column v1 (for example look at (v2,v3) = 10) Now look at the corresponding row and column in v1. (1, 13) Add the two numbers together. (1+13 = 14) If the sum is less than v’, replace v’ with the sum. (Is 14 < 13? No, so don’t replace it.)

v1v3v4v2 v1 v4 v3 v How about (v2, v4)? (1 + 2) = 3 Since this is less than 7, we replace the 7 with a 3. v1v3v4v2 v1 v4 v3 v Matrix Addition

What is the purpose of doing this?

v1v3v4v2 v1 v4 v3 v v1 v3 v2 1 7 v4 2 If we take the path from v2 to v4 it would cost us a total of 7 units However, if we move from v2 to v1. Then move from v1 to v4. The total cost would be 3 units. We have essentially found the shortest path at this moment from v2 to v4.

v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v Now do the same algorithm with row and column v2. After row and column v3. 5

v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v Finally, after row and column v4. 5 Although we did not see it in this example. It is possible a square gets updated more than once. After Floyd’s Algorithm is complete, we have the shortest path/cost path from any node to any other node in the graph. Time Complexity: O(V 3 )

Shortest Path Algorithms n Dijkstra’s Algorithm –Example of a greedy algorithm. –A greedy algorithm is one that takes the shortest path at any given moment without looking ahead. Many times a greedy algorithm is not the best choice. v1 v4 v3 v s Greedy path

Dijkstra’s Algorithm s Pick a starting point s and declare it known (True). We sill start with v1. known d v p v known is a true/false value. Whether we have delclared a vertex known (true) or not (false) d v is the cost from the previous vertex to the current vertex p v is the previous vertex in the path to the current vertex V v1 v2 v3 v4 v1 v3 v2 v4 s T 0 0 F 0 0

known d v p v V v1 v2 v3 v4 F 0 0 v1 v3 v2v v1 goes to v2 and v4. What is the cost of each edge? v1 to v2 = 9, v1 to v4 = 2, and v1 to v3 = 13 Now we declare 1 as known (True) and update d v and p v We have a choice now. Do we pick v2 or v4 to explore? known d v p v V v1 v2 v3 v4 T 0 0 F 9 v1 F 13 v1 F 2 v1

v1 v3 v2v We pick v4. Why? Greedy Algorithm We want the shortest path. v4 goes into v3 and v2 v4 to v3 = 4 v4 to v2 = 7 Now v4 is declared known. We update the table like before. known d v p v V v1 v2 v3 v4 T 0 0 F 7 v4 F 4 v4 T 2 v1 Next we explore the next vertex that led from v1. Namely v2

v1 v3 v2v v2 goes to v1 and v3 v2 to v1 = 1 v2 to v3 = 10 Now we declare v2 as known and update the table again. known d v p v V v1 v2 v3 v4 T 1 v2 T 7 v4 F 4 v4 T 2 v1 But wait! There is a problem. When updating v3 we see that the cost 10 is higher than the cost 4. We like the cost 4 so we will keep v3 the same.

v1 v3 v2v The last vertex to look at is v3. v3 goes to v2, v1 and v4. v3 to v2 = 2 v3 to v1 = 13 v3 to v4 = 14 v3 is now declared known. Like before we update as before, keeping in mind to keep lower cost paths. known d v p v V v1 v2 v3 v4 T 1 v2 T 2 v3 T 4 v4 T 2 v1

So How Do We Use This? known d v p v V v1 v2 v3 v4 T 1 v2 T 2 v3 T 4 v4 T 2 v1 Take any vertex you want as a starting point. Now locate that vertex in the p v column. Work backwards to the vertex you want to end up in. Easy! Lets look at an example: Let’s say we want the shortest path from v1 to v2. We see that v1 goes into v4 at a cost of 2. Now v4 goes into v3 at a cost of 4. Lastly v3 goes into v2 at a cost of = 8

v1 v3 v2v If we took a direct path from v1 to v2 it would cost us 9. But we found a way to get from v1 to v2 at a cost of 8. Time complexity of Dijkstra’s Algorithm is O(V 2 ) known d v p v V v1 v2 v3 v4 T 1 v2 T 2 v3 T 4 v4 T 2 v1