Module 11: Additional Topics Graph Theory and Applications

Similar documents
CS 206 Introduction to Computer Science II

CS302 - Data Structures using C++

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Inf 2B: Graphs, BFS, DFS

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Lecture 3. Brute Force

CSE 100: GRAPH ALGORITHMS

Trees. Arash Rafiey. 20 October, 2015

Announcements Problem Set 4 is out!

GRAPHS Lecture 19 CS2110 Spring 2013

Graphs and Algorithms

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

22.1 Representations of graphs

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Some major graph problems

LECTURE 17 GRAPH TRAVERSALS

Graphs & Digraphs Tuesday, November 06, 2007

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Graph Search Methods. Graph Search Methods

Understand graph terminology Implement graphs using

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

CS2 Algorithms and Data Structures Note 9

Graphs and Graph Algorithms. Slides by Larry Ruzzo

CS 206 Introduction to Computer Science II

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Breadth First Search. cse2011 section 13.3 of textbook

CSC263 Week 8. Larry Zhang.

(Re)Introduction to Graphs and Some Algorithms

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Fundamental Algorithms

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Algorithms and Data Structures, Academic Year 2010/2011

Graph Search Methods. Graph Search Methods

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

TIE Graph algorithms

CS302 Data Structures using C++

Graph traversal is a generalization of tree traversal except we have to keep track of the visited vertices.

22.3 Depth First Search

csci 210: Data Structures Graph Traversals

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Introduction to Algorithms. Lecture 11. Prof. Patrick Jaillet

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

EECS 203 Lecture 20. More Graphs

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

Copyright 2000, Kevin Wayne 1

Introduction to Graphs

An undirected graph G can be represented by G = (V, E), where

CS 310 Advanced Data Structures and Algorithms

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Algorithms: Lecture 10. Chalmers University of Technology

Worksheet for the Final Exam - Part I. Graphs

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

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

CS61BL. Lecture 5: Graphs Sorting

CS/COE

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Outline. Graphs. Divide and Conquer.

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Graph Traversals. Ric Glassey

CSE 373: Data Structures and Algorithms

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

Teach A level Compu/ng: Algorithms and Data Structures

TIE Graph algorithms

Graphs. The ultimate data structure. graphs 1

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

Lecture 9 Graph Traversal

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Lesson 1 Introduction to Path Planning Graph Searches: BFS and DFS

CSE 100: GRAPH SEARCH

GRAPHS Lecture 17 CS2110 Spring 2014

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Assignment No 2 (Group B)

Elements of Graph Theory

Notes for Lecture 24

CS 491 CAP Introduction to Graphs and Search

Graphs Introduction and Depth first algorithm

csci 210: Data Structures Graph Traversals

Algorithm Design and Analysis

W4231: Analysis of Algorithms

Graphs. Terminology. Graphs & Breadth First Search (BFS) Extremely useful tool in modeling problems. Consist of: Vertices Edges

CS213d Data Structures and Algorithms

CS 4407 Algorithms Lecture 5: Graphs an Introduction

Varying Applications (examples)

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

- Logic and Algorithms - Graph Algorithms

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Unit 2: Algorithmic Graph Theory

Transcription:

Module 11: Additional Topics Graph Theory and Applications Topics: Introduction to Graph Theory Representing (undirected) graphs Basic graph algorithms 1

Consider the following: Traveling Salesman Problem (TSP): Given N cities and the distances between them, find the shortest path to visit all cities and return to the start. 2

What does the TSP have in common with the following problems? Placement of new fire stations in a city to provide best coverage to all residents Ranking of "importance" of web pages by Google's PageRank algorithm Scheduling of final exams so they do not conflict Arranging components on a computer chip Analyzing strands of DNA Binary Search Trees 3

They all fall within the field of GRAPH THEORY Non-conflicting exams PageRank Algorithm 4

Undirected Simple Graphs An undirected simple graph G is a set V, of vertices, and a set E, of unordered distinct pairs from V, called edges. We write G=(V,E). 5

Graph Terminology If (vv kk, vv pp ) is an edge, we say that vv kk and vv pp are neighbours, and are adjacent. Note that kk and pp must be different. The number of neighbours of a vertex is also called its degree A sequence of nodes vv 1, vv 2,, vv kk is a path of length k-1 if (vv 1, vv 2 ),(vv 2, vv 3 ),,(vv kk 1, vv kk ) are all edges If vv 1 = vv kk, this is called a cycle A graph G is connected if there exists a path through all vertices in G 6

Interesting Results on Graphs Let nn = number of vertices, and mm = number of edges: 1. mm nn(nn 1)/2 2. The number of graphs on nn vertices is 2 n(n-1)/2 3. The sum of the degrees over all vertices is 2mm. 7

How can we store information about graphs in Python? We need to store labels for the vertices These could be strings or integers We need to store both endpoints using the labels on the vertices. We will consider three different implementations for undirected, unweighted graphs 8

Implementation 1: Vertex and Edge Lists V = [vv 1, vv 2, vv 3,, vv mm ], EE = [ee 1, ee 2, ee 3,, ee mm ], where edge ee jj = aa, bb when vertices aa and bb are connected by an edge V=[6,4,5,3,2,1] E=[[6,4], [4,5], [4,3], [3,2], [5,2], [1,2], [5,1]] 9

Implementation 2: Adjacency list For each vertex: Store the labels on its neighbours in a list We will use a dictionary Keys: labels of vertices Recall: integers or strings can be keys Associated values: List of neighbours (adjacent vertices) 10

Example: {1:[2,5], 2:[1,3,5], 3:[2,4], 4:[3,5,6], 5:[1,2,4], 6:[4]} 11

Implementation 3: Adjacency Matrix For simplicity, assume vertices are labelled 0,, nn 1 Create an nnnnnn matrix for G If there is an edge connecting ii and jj: Set G[i][j] = 1, Set G[j][i] = 1 Otherwise, set these values to 0 12

Example: G: vertex 0 1 2 3 4 5 0 [ [ 0, 1, 0, 0, 1, 0], 1 [ 1, 0, 1, 0, 1, 0], 2 [ 0, 1, 0, 1, 0, 0], 3 [ 0, 0, 1, 0, 1, 1], 4 [ 1, 1, 0, 1, 0, 0], 5 [ 0, 0, 0, 1, 0, 0]] 13

Comparing the implementations on simple tasks Determine if two vertices are neighbours. Find all the neighbours of a vertex. Which implementation to use? We'll use the adjacency list (a good case could also be made for the adjacency matrix). 14

Graph Traversals Determine all vertices of G that can be reached from a starting vertex There can be different types of traversals If you find all vertices starting from v, the graph is connected If not all vertices can be reached, a connected component containing v has been found Must determine a way to ensure we do not cycle indefinitely 15

Applications of traversals Finding path between two vertices Finding connected components Tracing garbage collection in programs (managing memory) Shortest path between two points Planarity testing Solving puzzles like mazes Graph colouring 16

One approach: Breadth-first search Traversal (bfs) Choose a starting point v Visit all the neighbours of v Then, visit all of the neighbours of the neighbours of v, etc. Repeat until all reachable vertices are visited Need some way to avoid visiting edges more than once Note: there may be more than one bfs ordering of a graph, starting from v. 17

Implementation of bfs traversal def bfs(graph, v): all = [] Q = [] Q.append(v) while Q!= []: v = Q.pop(0) all.append(v) for n in graph[v]: if n not in Q and\ n not in all: Q.append(n) return all 18

Starting bfs from 0 (1) Start from v=0 all = [] Q = [0] v = 0 all = [0] Neighbours of 0: 1,4 Q = [1,4] 19

Continuing bfs (2) Q = [1, 4] v = 1 all = [0,1] Neighbours of 1: 0,2,3 Q = [4,2,3] 20

Continuing bfs (3) Q: [4, 2, 3] v = 4 all = [0,1,4] Neighbours of 4: 0,3 No vertices added to Q Q= [2,3] v = 2 all = [0,1,4,2] Neighours of 2: 1,6 Q = [3,6] 21

Q: [3, 6] v = 3 all = [0,1,4,2,3] Neighbours of 3: 1,4,5 Q = [6,5] Q = [6, 5] v = 6 all = [0,1,4,2,3,6] Neighbours of 6: 2,7,8 Q = [5,7,8] Continuing bfs (4) 22

Continuing bfs (5) Q: [5, 7, 8] v = 5 all = [0,1,4,2,3,6,5] Neighbours of 5: 3,8 (Q unchanged) Q = [7,8] v = 7 all = [0,1,4,2,3,6,5,7] Neighbours of 7: 6 (Q unchanged) Q = [8] v = 8 all = [0,1,4,2,3,6,5,7,8] Neighbours of 8: 5,6 (Q unchanged) Q is empty 23

Another approach: depth-first traversal (dfs) Choose a starting point v Proceed along a path from v as far as possible Then, backup to previous (most recently visited) vertex, and visit its unvisited neighbour (this is called backtracking) Repeat while unvisited, reachable vertices remain Note: there may be more than one dfs ordering of a graph, starting from v. 24

A depth first search traversal solution def dfs(graph, v): visited = [] S = [v] while S!= []: v = S.pop() if v not in visited: visited.append(v) for w in graph[v]: if w not in visited: S.append(w) return visited 25

Breadth first vs depth first Searches Both need an additional list to store needed information: BFS uses Q: Add to the end and remove from the front Called a Queue DFS uses S: Add to the end and remove from the end Called a Stack Stacks and Queues are both very useful in CS 26

Extension: Weighted edges Each edge has an associated weight. It might represent: Distance between cities Cost to move between locations Capacity of a route Probability of moving from one web page to another 27

Adjust adjacency list to include weights Adjust our adjacency list to store weights with each edge {1:[[2,2],[4,5]], 2:[[1,2],[3,14], [4,5],[5,4]], 3:[[2,14],[5,34]], 4:[[1,5],[2,5],[5,58]], 5:[[2,4],[3,34],[4,58]]} 28

Other types of graphs Edges can be directed from one vertex to another Directed edges can have weights as well Exercise: Think about how directions change our representations 29

Goals of Module 11 Understand basic graph terminology Understand representation of graphs in Python Understand breadth-first and depth-first search traversals 30