1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Algorithms and Data Structures
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Chapter 8, Part I Graph Algorithms.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
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.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Connected Components, Directed Graphs, Topological Sort COMP171.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
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)
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Chapter 2 Graph Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
CSC 413/513: Intro to Algorithms Graph Algorithms.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graphs and Shortest Paths Using ADTs and generic programming.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Depth-First Search.
Graph.
Graph & BFS.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs Definition of Graphs and Related Concepts
Chapter 11 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 .
Presentation transcript:

1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department

2 Outline Reminder: Graphs Directed and undirected Matrix representation of graphs Directed and undirected Sparse matrices and sparse graphs Adjacency list representation

3 Graphs Tuple V is set of vertices E is a binary relation on V Each edge is a tuple, where v1,v2 in V |E| =< |V| 2

4 Directed and Undirected Graphs Directed graph: in E is ordered, i.e., a relation (v1,v2) Undirected graph: in E is un-ordered, i.e., a set { v1, v2 } Degree of a node X: Out-degree: number of edges In-degree: number of edges Degree: In-degree + Out-degree In undirected graph: number of edges { X, v2 }

5 Examples of graphs

6 Paths Path from vertex v 0 to vertex v k : A sequence of vertices For all 0 = exists. Path is of length k Two vertices x, y are adjacent if is an edge Path is simple if vertices in sequence are distinct. Cycle: if v 0 = v k is cycle of length 1

7 Connected graphs Undirected Connected graph: For any vertices x, y there exists a path x  y (= y  x) Directed connected graph: If underlying undirected graph is connected Strongly connected directed graph: If for any two vertices x, y there exist path x  y and path y  x Clique: a strongly connected component |V|-1 =< |E| =< |V| 2

8 Cycles and trees Graph with no cycles: acyclic Directed Acyclic Graph: DAG Undirected forest: Acyclic undirected graph Tree: undirected acyclic connected graph one connected component

9 Representing graphs Adjacency matrix: When graph is dense |E| close to |V| 2 Adjacency lists: When graph is sparse |E| << |V| 2

10 Adjacency Matrix Matrix of size |V| x |V| Each row (column) j correspond to a distinct vertex j “1” in cell if there is exists an edge Otherwise, “0” In an undirected graph, “1” in => “1” in “1” in means there’s a self-loop in vertex j

11 Examples

12 Adjacency matrix features Storage complexity: O(|V| 2 ) But can use bit-vector representation Undirected graph: symmetric along main diagonal A T transpose of A Undirected: A=A T In-degree of X: Sum along column X O(|V|) Out-degree of X: Sum along row X O(|V|) Very simple, good for small graphs Edge existence query: O(1)

13 But, …. Many graphs in practical problems are sparse Not many edges --- not all pairs x,y have edge x  y Matrix representation demands too much memory We want to reduce memory footprint Use sparse matrix techniques

14 Adjacency List An array Adj[ ] of size |V| Each cell holds a list for associated vertex Adj[u] is list of all vertices adjacent to u List does not have to be sorted Undirected graphs: Each edge is represented twice

15 Examples 1  3 2  2 3  1   2  3 2  1 3  1 4 

16 Adjacency list features Storage Complexity: O(|V| + |E|) In undirected graph: O(|V|+2*|E|) = O(|V|+|E|) Edge query check: O(|V|) in worst case Degree of node X: Out degree: Length of Adj[X] O(|V|) calculation In degree: Check all Adj[] lists O(|V|+|E|) Can be done in O(1) with some auxiliary information!

17 שאלות?

18 Graph Traversals (Search) We have covered some of these with binary trees Breadth-first search (BFS) Depth-first search (DFS) A traversal (search): An algorithm for systematically exploring a graph Visiting (all) vertices Until finding a goal vertex or until no more vertices Only for connected graphs

19 Breadth-first search One of the simplest algorithms Also one of the most important It forms the basis for MANY graph algorithms

20 BFS: Level-by-level traversal Given a starting vertex s Visit all vertices at increasing distance from s Visit all vertices at distance k from s Then visit all vertices at distance k+1 from s Then ….

21 BFS in a binary tree (reminder) BFS: visit all siblings before their descendents

22 BFS(tree t) 1. q  new queue 2. enqueue(q, t) 3. while (not empty(q)) 4. curr  dequeue(q) 5. visit curr // e.g., print curr.datum 6. enqueue(q, curr.left) 7. enqueue(q, curr.right) This version for binary trees only!

23 BFS for general graphs This version assumes vertices have two children left, right This is trivial to fix But still no good for general graphs It does not handle cycles Example.

24 Start with A. Put in the queue (marked red) ABGCEDF Queue: A

25 B and E are next ABGCEDF Queue: A B E

26 When we go to B, we put G and C in the queue When we go to E, we put D and F in the queue ABGCEDF Queue: A B E C G D F

27 When we go to B, we put G and C in the queue When we go to E, we put D and F in the queue ABGCEDF Queue: A B E C G D F

28 Suppose we now want to expand C. We put F in the queue again! ABGCEDF Queue: A B E C G D F F

29 Generalizing BFS Cycles: We need to save auxiliary information Each node needs to be marked Visited: No need to be put on queue Not visited:Put on queue when found What about assuming only two children vertices? Need to put all adjacent vertices in queue

30 BFS(graph g, vertex s) 1. unmark all vertices in G 2. q  new queue 3. mark s 4. enqueue(q, s) 5. while (not empty(q)) 6. curr  dequeue(q) 7. visit curr // e.g., print its data 8. for each edge 9. if V is unmarked 10. mark V 11. enqueue(q, V)

31 The general BFS algorithm Each vertex can be in one of three states: Unmarked and not on queue Marked and on queue Marked and off queue The algorithm moves vertices between these states

32 Handling vertices Unmarked and not on queue: Not reached yet Marked and on queue: Known, but adjacent vertices not visited yet (possibly) Marked and off queue: Known, all adjacent vertices on queue or done with

33 Start with A. Mark it. ABGCEDF Queue: A

34 Expand A’s adjacent vertices. Mark them and put them in queue. ABGCEDF Queue: A B E

35 Now take B off queue, and queue its neighbors. ABGCEDF Queue: A B E C G

36 Do same with E. ABGCEDF Queue: A B E C G D F

37 Visit C. Its neighbor F is already marked, so not queued. ABGCEDF Queue: A B E C G D F

38 Visit G. ABGCEDF Queue: A B E C G D F

39 Visit D. F, E marked so not queued. ABGCEDF Queue: A B E C G D F

40 Visit F. E, D, C marked, so not queued again. ABGCEDF Queue: A B E C G D F

41 Done. We have explored the graph in order: A B E C G D F. ABGCEDF Queue: A B E C G D F

42 Interesting features of BFS Complexity: O(|V| + |E|) All vertices put on queue exactly once For each vertex on queue, we expand its edges In other words, we traverse all edges once BFS finds shortest path from s to each vertex Shortest in terms of number of edges Why does this work?

43 Depth-first search Again, a simple and powerful algorithm Given a starting vertex s Pick an adjacent vertex, visit it. Then visit one of its adjacent vertices ….. Until impossible, then backtrack, visit another

44 DFS(graph g, vertex s) Assume all vertices initially unmarked 1. mark s 2. visit s // e.g., print its data 3. for each edge 4. if V is not marked 5. DFS(G, V)

45 Start with A. Mark it. ABGCEDF Current vertex: A

46 Expand A’s adjacent vertices. Pick one (B). Mark it and re-visit. ABGCEDF Current: B

47 Now expand B, and visit its neighbor, C. ABGCEDF Current: C

48 Visit F. Pick one of its neighbors, E. ABGCEDF Current: F

49 E’s adjacent vertices are A, D and F. A and F are marked, so pick D. ABGCEDF Current: E

50 Visit D. No new vertices available. Backtrack to E. Backtrack to F. Backtrack to C. Backtrack to B ABGCEDF Current: D

51 Visit G. No new vertices from here. Backtrack to B. Backtrack to A. E already marked so no new. ABGCEDF Current: G

52 Done. We have explored the graph in order: A B C F E D G ABGCEDF Current:

53 Interesting features of DFS Complexity: O(|V| + |E|) All vertices visited once, then marked For each vertex on queue, we examine all edges In other words, we traverse all edges once DFS does not necessarily find shortest path Why?