CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Jiafen Liu Sept
Advertisements

Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy.
Chapter 23 Minimum Spanning Trees
Graph & BFS.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Graphs Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Subchap 22.1 – Representation of Graphs.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
1/24 Introduction to Graphs. 2/24 Graph Definition Graph : consists of vertices and edges. Each edge must start and end at a vertex. Graph G = (V, E)
CSC 413/513: Intro to Algorithms Graph Algorithms.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
CSC2100B Tutorial 10 Graph Jianye Hao.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
CS 201: Design and Analysis of Algorithms
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Algorithms
Graphs Algorithm Design and Analysis Bibliography:
Topological Sort Minimum Spanning Tree
CS 3343: Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Lecture 12 Algorithm Analysis
CS 3343: Analysis of Algorithms
CS120 Graphs.
Graph Algorithm.
Elementary Graph Algorithms
Intro to Graph Algorithms (Slides originally created by David Luebke)
CSCE350 Algorithms and Data Structure
Modeling and Simulation NETW 707
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Graphs Chapter 15 explain graph-based algorithms Graph definitions
CS6045: Advanced Algorithms
CSE 373 Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Introduction to Algorithms: Greedy Algorithms (and Graphs)
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Lecture 12 Algorithm Analysis
Algorithm Course Dr. Aref Rashad
Graphs Algorithm Design and Analysis Bibliography:
Minimum Spanning Trees
Presentation transcript:

CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs

Uniform-profit Restaurant location problem d Goal: maximize number of restaurants open Subject to: distance constraint (min-separation >= 10)

Events scheduling problem Time e1e1 e2e2 e3e3 e4e4 e5e5 e6e6 e7e7 e8e8 e9e9 Goal: maximize number of non-conflicting events

Fractional knapsack problem Goal: maximize value without exceeding bag capacity Weight limit: 10LB $ / LB Value ($) Weight (LB) item

Example Goal: maximize value without exceeding bag capacity Weight limit: 10LB = 10 LB *2 = 15.4 itemWeight (LB) Value ($) $ / LB

The remaining lectures Graph algorithms Very important in practice –Tons of computational problems can be defined in terms of graphs –We’ll study a few interesting ones Minimum spanning tree Shortest path Graph search Topological sort, connected components

Graphs A graph G = (V, E) –V = set of vertices –E = set of edges = subset of V  V –Thus |E| = O(|V| 2 ) Vertices: {1, 2, 3, 4} Edges: {(1, 2), (2, 3), (1, 3), (4, 3)}

Graph Variations (1) Directed / undirected: –In an undirected graph: Edge (u,v)  E implies edge (v,u)  E Road networks between cities –In a directed graph: Edge (u,v): u  v does not imply v  u Street networks in downtown –Degree of vertex v : The number of edges adjacency to v For directed graph, there are in-degree and out-degree

Directed Undirected Degree = 3 In-degree = 3 Out-degree = 0

Graph Variations (2) Weighted / unweighted: –In a weighted graph, each edge or vertex has an associated weight (numerical value) E.g., a road map: edges might be weighted w/ distance Unweighted Weighted

Graph Variations (3) Connected / disconnected: –A connected graph has a path from every vertex to every other –A directed graph is strongly connected if there is a directed path between any two vertices Connected but not strongly connected

Graph Variations (4) Dense / sparse: –Graphs are sparse when the number of edges is linear to the number of vertices |E|  O(|V|) –Graphs are dense when the number of edges is quadratic to the number of vertices |E|  O(|V| 2 ) –Most graphs of interest are sparse –If you know you are dealing with dense or sparse graphs, different data structures may make sense

Representing Graphs Assume V = {1, 2, …, n} An adjacency matrix represents the graph as a n x n matrix A: –A[i, j] = 1 if edge ( i, j )  E = 0 if edge (i, j)  E For weighted graph –A[i, j] = w ij if edge ( i, j )  E = 0 if edge (i, j)  E For undirected graph –Matrix is symmetric: A[i, j] = A[j, i]

Graphs: Adjacency Matrix Example: A ?? 4

Graphs: Adjacency Matrix Example: A How much storage does the adjacency matrix require? A: O(V 2 )

Graphs: Adjacency Matrix Example: A Undirected graph

Graphs: Adjacency Matrix Example: A Weighted graph

Graphs: Adjacency Matrix Time to answer if there is an edge between vertex u and v: Θ(1) Memory required: Θ(n 2 ) regardless of |E| –Usually too much storage for large graphs –But can be very efficient for small graphs Most large interesting graphs are sparse –E.g., road networks (due to limit on junctions) –For this reason the adjacency list is often a more appropriate representation

Graphs: Adjacency List Adjacency list: for each vertex v  V, store a list of vertices adjacent to v Example: –Adj[1] = {2,3} –Adj[2] = {3} –Adj[3] = {} –Adj[4] = {3} Variation: can also keep a list of edges coming into vertex

Graph representations Adjacency list How much storage does the adjacency list require? A: O(V+E)

Graph representations Undirected graph A

Graph representations Weighted graph A 2,53,6 1,53,9 3,4 1,62,94,4

Graphs: Adjacency List How much storage is required? For directed graphs –|adj[v]| = out-degree(v) –Total # of items in adjacency lists is  out-degree(v) = |E| For undirected graphs –|adj[v]| = degree(v) –# items in adjacency lists is  degree(v) = 2 |E| So: Adjacency lists take  (V+E) storage Time needed to test if edge (u, v)  E is O(n)

Tradeoffs between the two representations Adj MatrixAdj List test (u, v)  E Θ(1)O(n) Degree(u)Θ(n)O(n) MemoryΘ(n 2 )Θ(n+m) Edge insertionΘ(1) Edge deletionΘ(1)O(n) Graph traversalΘ(n 2 )Θ(n+m) |V| = n, |E| = m Both representations are very useful and have different properties.

Minimum Spanning Tree Problem: given a connected, undirected, weighted graph:

Minimum Spanning Tree Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight A spanning tree is a tree that connects all vertices Number of edges = ? A spanning tree has no designated root.

How to find MST? Connect every node to the closest node? –Does not guarantee a spanning tree

Minimum Spanning Tree MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees –Let T be an MST of G with an edge (u,v) in the middle –Removing (u,v) partitions T into two trees T 1 and T 2 –w(T) = w(u,v) + w(T 1 ) + w(T 2 ) Claim 1: T 1 is an MST of G 1 = (V 1, E 1 ), and T 2 is an MST of G 2 = (V 2, E 2 ) T1T1 T2T2 u v Proof by contradiction: if T 1 is not optimal, we can replace T 1 with a better spanning tree, T 1 ’ T 1 ’, T 2 and ( u, v ) form a new spanning tree T’ W(T’) < W(T). Contradiction. T1’T1’

Minimum Spanning Tree MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees –Let T be an MST of G with an edge (u,v) in the middle –Removing (u,v) partitions T into two trees T 1 and T 2 –w(T) = w(u,v) + w(T 1 ) + w(T 2 ) Claim 2: (u, v) is the lightest edge connecting G 1 = (V 1, E 1 ) and G 2 = (V 2, E 2 ) T1T1 T2T2 u v Proof by contradiction: if (u, v) is not the lightest edge, we can remove it, and reconnect T 1 and T 2 with a lighter edge ( x, y ) T 1, T 2 and ( x, y ) form a new spanning tree T’ W(T’) < W(T). Contradiction. x y

Algorithms Generic idea: –Compute MSTs for sub-graphs –Connect two MSTs for sub-graphs with the lightest edge Two of the most well-known algorithms –Prim’s algorithm –Kruskal’s algorithm –Let’s first talk about the ideas behind the algorithms without worrying about the implementation and analysis

Prim’s algorithm Basic idea: –Start from an arbitrary single node A MST for a single node has no edge –Gradually build up a single larger and larger MST Fully explored nodes Discovered but not fully explored nodes Not yet discovered

Prim’s algorithm Basic idea: –Start from an arbitrary single node A MST for a single node has no edge –Gradually build up a single larger and larger MST Fully explored nodes Discovered but not fully explored nodes Not yet discovered

Prim’s algorithm Basic idea: –Start from an arbitrary single node A MST for a single node has no edge –Gradually build up a single larger and larger MST

Prim’s algorithm in words Randomly pick a vertex as the initial tree T Gradually expand into a MST: –For each vertex that is not in T but directly connected to some nodes in T Compute its minimum distance to any vertex in T –Select the vertex that is closest to T Add it to T

Example a a b b f f c c e e d d g g h h

Example a a b b f f c c e e d d g g h h

Example a a b b f f c c e e d d g g h h

Example a a b b f f c c e e d d g g h h

Example a a b b f f c c e e d d g g h h

Example a a b b f f c c e e d d g g h h

Example a a b b f f c c e e d d g g h h

Example a a b b f f c c e e d d g g h h Total weight = = 53

Kruskal’s algorithm Basic idea: –Grow many small trees –Find two trees that are closest (i.e., connected with the lightest edge), join them with the lightest edge –Terminate when a single tree forms

Claim If edge (u, v) is the lightest among all edges, (u, v) is in a MST Proof by contradiction: –Suppose that (u, v) is not in any MST –Given a MST T, if we connect (u, v), we create a cycle –Remove an edge in the cycle, have a new tree T’ –W(T’) < W(T) u v By the same argument, the second, third, …, lightest edges, if they do not create a cycle, must be in MST

Kruskal’s algorithm in words Procedure: –Sort all edges into non-decreasing order –Initially each node is in its own tree –For each edge in the sorted list If the edge connects two separate trees, then –join the two trees together with that edge

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15

Example a a b b f f c c e e d d g g h h c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15