1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I.

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Graphs. Data structures that connect a set of objects to form a kind of a network Objects are called “Nodes” or “Vertices” Connections are called “Edges”
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
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 Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
Graph & BFS.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
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.
Introduction to Graphs
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.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Chapter 9: Graphs Basic Concepts
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
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
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.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
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|
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
Graph. 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.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
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 05 Introduction to Graph And Search Algorithms.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
Data Structures, Algorithms & Complexity
CSC317 Graph algorithms Why bother?
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Graphs Representation, BFS, DFS
Chapter 9: Graphs Basic Concepts
Basic Graph Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Chapter 9: Graphs Basic Concepts
Presentation transcript:

1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I

2 Objects and Connections Many problems are naturally formulated in terms of objects and the connections between them. For example: Airline Routes--What is the fastest (or cheapest) way to get from one city to another? Electrical circuits--Circuit elements are wired together. How does the current flow? Job Scheduling--The objects are tasks that need to be performed. The connections indicate which jobs should be done before which other jobs. Web site design--How are the pages in the site connected? A graph is a mathematical object that describes such situations. Algorithms for graphs are fundamental to CS Graph Theory is a major branch of combinatorial mathematics.

3 Graphs A graph is a collection of vertices, V, and edges, E. An edge connects two vertices. a d cb ad cb is the same as: Vertices: a, b, c, d Edges: ab, bc, ac, ad

4 Definitions A path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph. ad cb Example: A path from a to c could be ac or abc. A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected. ad cb e Not Connected

5 More definitions A simple path is a path with no vertex repeated. A simple cycle is a simple path except the first and last vertex is repeated and, for an undirected graph, number of vertices >= 3. ad cb Example: abca is a cycle A tree is a graph with no cycles. ad cb

6 Definitions Continued A complete graph is a graph in which all edges are present. A sparse graph is a graph with relatively few edges. A dense graph is a graph with many edges. ad cb ad cb ad cb Complete DenseSparse

7 Types of Graphs An undirected graph has no specific direction between the vertices. A directed graph has edges that are "one way". We can go from one vertex to another, but not vice versa. A weighted graph has weights associated with each edge. The weights can represent distances, costs, etc. ad cb ad cb ad cb UndirectedDirectedWeighted

8 Representing Graphs as an Adjacency List An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge List

9 Representing a Graph with an Adjacency Matrix An adjacency matrix is a matrix with a row and column for each vertex. The matrix entry is 1 if there is an edge between the row vertex and the column vertex The diagonal may be zero or one. Each edge is represented twice.

10 Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex List

11 Representing Weighted Graphs In weighted graphs we include the weights of each edge List

12 Lists vs. Matrices Speed: An adjacency matrix provides the fastest way to determine whether or not a particular edge is present in the graph. For an adjacency list, there is no faster way than to search the entire list for the presence of the edge. Memory: Normally, an adjacency matrix requires more space (n 2 entries). However, if n is small and the graph is unweighted, an adjacency matrix only needs 1 bit per entry. The adjacency list requires at least 1 word per entry (for the address of the next node of the list). This may offset the advantage of fewer entries.

13 Breadth First Search (BFS) Problem: Given a graph, G = (V, E), find all the vertices reachable from some source, s. Compute the distance (shortest path) from s to each vertex. Produce a tree such that: s is the root of the tree The nodes of the tree represent all reachable vertices The path from s to v corresponds to the path containing the fewest edges from s to v in the graph.

14 Strategy for BFS As we search the graph we: Keep track of "discovered" and "undiscovered" vertices. Maintain a frontier of discovered vertices that spreads out across the graph. Use three colors: White = undiscovered vertex Gray= Discovered vertex, but we don't know all its connections Black= Discovered vertex and we know all its connections.

15 Strategy continued For each gray vertex starting with s: Find all undiscovered vertices reachable by 1 edge from that vertex and color them gray. Add them to the tree below v. Color the original vertex black. Process all vertices at level k (distance k from s) before processing the vertices at level k+1

Pseudocode for BFS BFS(G, s){G is graph, s is source vertex} for v in vertices(G) do{Initialize each vertex to white} color[v] <- white d[v] <- infinity parent[v] <- nil colors[s] <- gray{s is discovered} d[s] <- 0 parent[s] <- nil Q <- Enqueue(s, Empty-Queue){Start with s in the queue} while not Empty-Queue?(Q) do f <- Dequeue(Q){Process next element} for g in Adj[f] do{Check all adjacent vertices} if color[g] = white then{If undiscovered discover it} color[g] <- gray d[g] <- d[f] + 1 parent[g] <-f Enqueue(g, Q){Add it to the queue} color[f] <- black{Once adjacent vertices discovered, color this one black}

17 Example rstu yxwv We will work through this in class.

18 Analysis Initialization:  (V) While loop: Each vertex enqueued and dequeued once:  (V) Adjacency list scanned once for each vertex. Each edge is represented twice in all lists. Sum of the lengths of all lists = 2E, so The time to process the adjacency lists will be  (E) Total for while loop =  (V) +  (E) =  (V + E) Total =  (V) +  (V+E) =  (V+E)