CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

Similar documents
CS 361 Data Structures & Algs Lecture 9. Prof. Tom Hayes University of New Mexico

Solutions to relevant spring 2000 exam problems

Graph Theory S 1 I 2 I 1 S 2 I 1 I 2

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

implementing the breadth-first search algorithm implementing the depth-first search algorithm

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

Algorithms: Lecture 10. Chalmers University of Technology

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

Reference Sheet for CO142.2 Discrete Mathematics II

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Graph and Digraph Glossary

Chapter 9. Priority Queue

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Algorithm Design and Analysis

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

CS4800: Algorithms & Data Jonathan Ullman

Chapter 6 Heapsort 1

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

Introduction to Algorithms and Data Structures. Lecture 13: Data Structure (4) Data structures for graphs and example in binary search tree

CISC 320 Midterm Exam

Binary Relations McGraw-Hill Education

A loose end: binary search

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Discrete mathematics II. - Graphs

CS 561, Lecture 10. Jared Saia University of New Mexico

Algorithms: Graphs. Amotz Bar-Noy. Spring 2012 CUNY. Amotz Bar-Noy (CUNY) Graphs Spring / 95

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD


Math 776 Graph Theory Lecture Note 1 Basic concepts

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

A graph is finite if its vertex set and edge set are finite. We call a graph with just one vertex trivial and all other graphs nontrivial.

Minimum Spanning Trees

Data Structure. IBPS SO (IT- Officer) Exam 2017

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Goals! CSE 417: Algorithms and Computational Complexity!

CS 206 Introduction to Computer Science II

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Graphs & Digraphs Tuesday, November 06, 2007

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

CS70 - Lecture 6. Graphs: Coloring; Special Graphs. 1. Review of L5 2. Planar Five Color Theorem 3. Special Graphs:

Line segment intersection. Family of intersection problems

Copyright 2000, Kevin Wayne 1

CS61BL. Lecture 5: Graphs Sorting

Algorithm Design (8) Graph Algorithms 1/2

Algorithms. Deleting from Red-Black Trees B-Trees

Introduction to Computers and Programming. Concept Question

CSC Intro to Intelligent Robotics, Spring Graphs

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Slides for Faculty Oxford University Press All rights reserved.

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV

Advanced Set Representation Methods

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Algorithms. Graphs. Algorithms

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Trees. A tree is a directed graph with the property

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Solutions to Exam Data structures (X and NV)

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Algorithm Design and Analysis

Graphs. CS16: Introduction to Data Structures & Algorithms Spring 2018

CSE 100: GRAPH ALGORITHMS

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CS 561, Lecture 10. Jared Saia University of New Mexico

Data and File Structures Laboratory

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Math 778S Spectral Graph Theory Handout #2: Basic graph theory

CSI 604 Elementary Graph Algorithms

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

2. Lecture notes on non-bipartite matching

Lecture 6,

22.1 Representations of graphs

Graph Algorithms Using Depth First Search

CS2 Algorithms and Data Structures Note 6

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao,David Tse Note 8

Chapter 11: Graphs and Trees. March 23, 2008

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Graphs and Graph Algorithms. Slides by Larry Ruzzo

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Treewidth and graph minors

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

1 Matchings in Graphs

CMSC 380. Graph Terminology and Representation

Bipartite Roots of Graphs

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

HW Graph Theory SOLUTIONS (hbovik) - Q

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

CS 310 Advanced Data Structures and Algorithms

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Lecture 10: Strongly Connected Components, Biconnected Graphs

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Transcription:

CS 361 Data Structures & Algs Lecture 11 Prof. Tom Hayes University of New Mexico 09-28-2010 1

Last Time Priority Queues & Heaps Heapify (up and down) 1: Preserve shape of tree 2: Swaps restore heap order property Balanced Binary Tree using Array Quiz #2 New Reading: secs 3.1 thru 3.4 2

Quiz 2 grades 20, 20, 20, 20, 19, 17 16, 16, 15, 15, 15 14, 14, 14, 14, 13 12, 12, 12 11, 11, 11, 11 10, 9, 9, 9 7, 7, 3, 0 3

Today P.A. 2 due Monday, Oct 11 Graphs and Trees, terminology Connectedness, Components Traversal Algorithms Breadth First vs. Depth First Testing Bipartiteness 4

Graphs A graph is a pair, (V,E), where: V is the set of vertices (also called nodes ) E is a set of edges Each edge consists of a pair of vertices, called the endpoints of the edge. Example: V = {1,2,3,4,5}, E = { {1,2}, {2,3}, {3,4}, {4,5}, {5,1}, {1,4} }. (5 vertices, 6 edges). 5

Kinds of Graphs Vertices: can represent almost anything. Cities, people, computers, numbers. Edges: represent some notion of adjacency or relationships like knowing, meeting, liking, being similar to. Anything that can involve (or not involve) a pair of vertices. Sometimes: we also want to attach weights to the edges and/or the vertices. But not for today. 6

Drawing a graph A diagram of a graph is a picture, with a dot for each vertex, and a segment for each edge. Example: V = {1,2,3,4,5}, E = { {1,2}, {2,3}, {3,4}, {4,5}, {5,1}, {1,4} }. (5 vertices, 6 edges). 1 5 2 4 3 7

Paths & Connectedness A graph is connected if, for any two nodes v,w, there is a sequence of edges that joins v to w. A minimal such sequence of edges is called a path from v to w. Example: one path from 1 to 4 is {1,2}, {2,3}, {3,4}. Another is {1,4}. A third is 1 5 6 2 {1,5},{5,4}. {2,6} is on no path 4 3 8

Components Two nodes in a graph are said to be in the same connected component if there exists a path joining them. Claim: this is an equivalence relation. Why? Consequence: Every graph decomposes in a unique way into its connected components. Obs: G is connected iff G has only one connected component. 9

Equivalence Relations Let ~ be a binary relation on a set S. (For elements a, b in S, a ~ b is a proposition which can be true or false.) We say ~ is an equivalence relation if 3 axioms hold: 1) reflexive. a ~ a is always true. 2) symmetric. a ~ b is equivalent to b ~ a 3) transitive. If a~b and b~c, then a~c. 10

Equivalence Classes Suppose ~ is an equivalence relation on S. Then S can be decomposed into subsets S1, S2, S3, etc. called equivalence classes, meaning: For all a,b in same equiv. class Si, we have a~b. For all a in Si, b in Sj, i j, we have NOT(a~b). Equivalence classes are an alternative way of defining an equivalence relation. 11

Components a, b : vertices in graph G. a ~ b : there is a path from a to b Equivalence relation. Equivalence class containing a: All vertices that can be reached by a path from a. Connected component containing a. # of connected components? 12

Components a, b : vertices in graph G. a ~ b : there is a path from a to b Equivalence relation. Equivalence class containing a: All vertices that can be reached by a path from a. Connected component containing a. # of connected components? Between 1 and n, where n=#vertices in G. 13

Cycles Def: A cycle in a graph is a closed loop with no repeated edges or nodes (except the start and end). Example: In this graph, (1,2,3,4,5) is a cycle. So is (1,4,5). So is (1,4,3,2). 1 5 6 2 4 3 14

Trees & Forests Def: A forest is a graph with no cycles. Def: A tree is a connected graph with no cycles. Remark: Every forest is a union of trees. A tree is a special case of a forest. Example: a tree. 1 5 6 2 4 3 15

Trees & Forests Def: A forest is a graph with no cycles. Def: A tree is a connected graph with no cycles. Remark: Every forest is a union of trees. A tree is a special case of a forest. Example: a forest. 1 5 6 2 4 3 16

Recall: Binary Trees root Data is stored in nodes. Each node has 4 fields: data leaf leaf leaf parent (either a ref to a node, or null ) left_child (reference to a node or null) right_child (reference to a node or null) The graph for a binary tree is a tree. (Connected, no cycles) leaf 17

Rooted Trees A binary tree has a special node called the root. Every node, v, has a unique path to the root. parent(v) is the first node along this path. In a general tree, any node can be made the root. This is called rooting 1 5 6 2 4 3 18

Rooted Trees A binary tree has a special node called the root. Every node, v, has a unique path to the root. parent(v) is the first node along this path. In a general tree, any node can be made the root. This is called rooting 1 5 6 2 4 3 19

Rooted Trees A binary tree has a special node called the root. Every node, v, has a unique path to the root. parent(v) is the first node along this path. In a general tree, any node can be made the root. This is called rooting 1 5 2 4 3 6 20

Rooted Trees A binary tree has a special node called the root. Every node, v, has a unique path to the root. parent(v) is the first node along this path. In a general tree, any node can be made the root. This is called rooting 1 5 6 2 4 3 21

Rooted Trees A binary tree has a special node called the root. Every node, v, has a unique path to the root. parent(v) is the first node along this path. In a general tree, any node can be made the root. This is called rooting 6 2 1 3 5 4 22

Testing Connectedness Input: A graph G, and vertices s,t. Output: A path from s to t, if one exists, and otherwise output Disconnected How do we proceed? First issue: How do we store a graph in the computer? 23

Storing a Graph 2 main approaches: (a) Adjacency list representation. (Better) (b) Adjacency matrix. (Worse) 24

Adjacency List repn Graph: int N = how many vertices there are. Adj[ v ] = A List of the neighbors of v. So: we have an Array of Linked Lists. Example: V = {1,2,3,4,5}, E = { {1,2}, {2,3}, {3,4}, {4,5}, {5,1}, {1,4}, {1,3} }. (5 vertices, 7 edges). 25

Adjacency List repn Example: V = {1,2,3,4,5}, E = { {1,2}, {2,3}, {3,4}, {4,5}, {5,1}, {1,4}, {1,3} }. (5 vertices, 7 edges). 1 Adj[1] = {2, 5, 4, 3} Adj[2] = {1, 3} Adj[3] = {2, 4, 1} Adj[4] = {3, 5, 1} 5 2 Adj[5] = {4, 1} 4 3 26

Adjacency Matrix repn Graph: int N = how many vertices there are. A = n x n matrix of 0 s and 1 s A[i,j] = 1 means the edge {i, j} is included. 27

Adjacency List repn Example: V = {1,2,3,4,5}, E = { {1,2}, {2,3}, {3,4}, {4,5}, {5,1}, {1,4}, {1,3} }. (5 vertices, 7 edges). 1 A = 0 1 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0 1 5 2 1 0 0 1 0 4 3 28

Adjacency List repn Example: V = {1,2,3,4,5}, E = { {1,2}, {2,3}, {3,4}, {4,5}, {5,1}, {1,4}, {1,3} }. (5 vertices, 7 edges). 1 A = 0 1 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0 1 5 2 1 0 0 1 0 4 3 29

Adjacency Matrix repn Graph: int N = how many vertices there are. A = n x n matrix of 0 s and 1 s A[i,j] = 1 means the edge {i, j} is included. Why is this worse than Adjacency List (in general)? 30

Adjacency Matrix repn Graph: int n = how many vertices there are. A = n x n matrix of 0 s and 1 s A[i,j] = 1 means the edge {i, j} is included. Why is this worse than Adjacency List (in general)? Always requires n^2 space (and n^2 time to read/ write it). So what? How many edges can there be? 31

Adjacency Matrix repn Graph: int n = how many vertices there are. A = n x n matrix of 0 s and 1 s A[i,j] = 1 means the edge {i, j} is included. Why is this worse than Adjacency List (in general)? Always requires n^2 space (and n^2 time to read/ write it). So what? How many edges can there be? There can be ( ) n 2 = Θ(n 2 ) 32

Adjacency Matrix repn Graph: int n = how many vertices there are. A = n x n matrix of 0 s and 1 s A[i,j] = 1 means the edge {i, j} is included. Why is this worse than Adjacency List (in general)? Always requires n^2 space (and n^2 time to read/ write it). So what? How many edges can there be? There can be ( ) n 2 = Θ(n 2 ) 33

How many edges? Suppose G is a graph, with N vertices. What is the fewest edges G can have? 34

How many edges? Suppose G is a graph, with N vertices. What is the fewest edges G can have? Zero. 35

How many edges? Suppose G is a graph, with N vertices. What is the fewest edges G can have? Zero. Suppose G is a connected graph with N vertices. What is the fewest edges G can have? 36

How many edges? Suppose G is a graph, with N vertices. What is the fewest edges G can have? Zero. Suppose G is a connected graph with N vertices. What is the fewest edges G can have? N-1. Proof? 37

How many edges? Suppose G is a graph, with N vertices. What is the fewest edges G can have? Zero. Suppose G is a connected graph with N vertices. What is the fewest edges G can have? N-1. Proof? Induction: Start with empty graph. Then there are N connected components. Each edge we add can reduce the number of components by 0 or by 1. So it takes at least N-1 edges to make G connected. 38

How many edges? Suppose G is a graph, with N vertices. What is the fewest edges G can have? Zero. Suppose G is a connected graph with N vertices. What is the fewest edges G can have? N-1. In this case, G is always a tree! 39

How many edges? Suppose G is a graph, with N vertices. What is the fewest edges G can have? Zero. Suppose G is a connected graph with N vertices. What is the fewest edges G can have? N-1. In this case, G is always a tree! What is the most edges G can have? ( N 2 ) = N(N 1) 2 = Θ(N 2 ) 40

Testing Connectivity Input: A graph G, and vertices s,t. Output: A path from s to t, if one exists, and otherwise output Disconnected How do we proceed? Start at s, and search outward 41

Testing Connectivity Input: A graph G, and vertices s,t. Output: A path from s to t, if one exists, and otherwise output Disconnected How do we proceed? Start at s, and search outward Build up a tree, rooted at s, as we go. Eventually, we will find all nodes in the component of s. If t is there, the path from t to s is t, parent(t), parent(parent(t)),..., s 42