Graphs Motivation and Terminology Representations Traversals Variety of Problems.

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Review Binary Search Trees Operations on Binary Search Tree
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
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:
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
Graph & BFS.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
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.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
Social Media Mining Graph Essentials.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
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.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
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.
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.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Properties and Applications of Depth-First Search Trees and Forests
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.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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.
Graphs Motivation and Terminology Representations Traversals
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
CS120 Graphs.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Graphs Motivation and Terminology Representations Traversals Variety of Problems

Graphs A graph G consists of a set of vertices V together with a set E of vertex pairs or edges. G = (V,E) [in some texts they use G(V,E)]. We also use V and E to represent # of nodes, edges Graphs are important because any binary relation is a graph, so graphs can be used to represent essentially any relationship.

Graph Interpretations Living Room Den Bedroom Kitchen Hallway Bath Living Den Room Hall Bed Kitchen Bath Apartment Blueprint The vertices could represent rooms in a house, and the edges could indicate which of those rooms are connected to each other. Sometimes a using a graph will be an easy simplification for a problem.

More interpretations Vertices are cities and edges are the roads connecting them. Edges are the components in a circuit and vertices are junctions where they connect. Vertices are software packages and edges indicate those that can interact. Edges are phone conversations and vertices are the households being connected.

Friendship Graphs Auntie M. Tin Man The Wicked Witch of the West Each vertex represents a person, and each edge indicates that the two people are friends. DorothyScarecrow Cowardly Lion

Graph Terminology Directed and undirected graphs A graph is said to be undirected if edge (x, y) always implies (y, x). Otherwise it is said to be directed. Often called an arc. Loops, multiedges, and simple graphs An edge of the form (x, x) is said to be a loop. If x was y’s friend several times over, we can model this relationship using multiedges. A graph is said to be simple if it contains no loops or multiedges. Weighted edges A graph is said to be weighted if each edge has an associated numerical attribute. In an unweighted graph, all edges are assumed to be of equal weight.

Terminology continued... Paths A path is a any sequence of edges that connect two vertices. A simple path never goes through any vertex more than once. The shortest path is the minimum number edges needed to connect two vertices. Connectivity The “six degrees of separation” theory argues that there is always a short path between any two people in the world. A graph is connected if there is there is a path between any two vertices. A directed graph is strongly connected if there is always a directed path between vertices. Any subgraph that is connected can be referred to as a connected component.

Still More Terminology... Degree and graph types The degree of a vertex is the number of edges connected to it. The most popular person will have a vertex of the highest degree. Remote hermits may have degree-zero vertices. In dense graphs, most vertices have high degree. In sparse graphs, most vertices have low degree. In a regular graph, all vertices have exactly the same degree. Clique A graph is called complete if every pair of vertices is connected by an edge. A clique is a sub-graph that is complete.

Yet More Terminology... Cycles and Dags A cycle is a path where the last vertex is adjacent to the first. A cycle in which no vertex is repeated is said to be a simple cycle. The shortest cycle in a graph determines the graph’s girth. A simple cycle that passes through every vertex is said to be a Hamiltonian cycle. An undirected graph with no cycles is a tree if it is connected, or a forest if it is not. A directed graph with no directed cycles is said to be a directed acyclic graph (or a DAG)

Graphs Motivation and Terminology Representations Traversals Variety of Problems

Adjacency Matrices

Adjacency Lists

Tradeoffs Between Adjacency Lists and Adjacency Matrices ComparisonWinner (for worst case) Faster to test if (x, y) exists? matrices:  (1) vs.  (V) Faster to find vertex degree?lists:  (1) vs.  (V) Less memory on sparse graphs? lists:  (V+E) vs.  (V 2 ) Less memory on dense graphs? matrices: (small win) Edge insertion or deletion? matrices:  (1) vs.  (V) Faster to traverse the graph? lists:  (E+V) vs.  (V 2 ) Better for most problems? lists

Graph Squaring The square of a directed graph G = (V, E) is the graph G 2 = (V, E 2 ), such that (x, y)  E 2 iff, for some z, both (x, z) and (z, y)  E ; i.e., there is a path of exactly two edges. Give efficient algorithms to square a graph on both adjacency lists and matrices.

G 2 with Adjacency Matrices Given an adjacency matrix, we can check in constant time whether a given edge exists. To discover whether there is an edge (x, y) in E 2, for each possible intermediate vertex z we can check whether (x, z) and (z, y) exist in O(1). Since there are O(V) intermediate vertices to check, and O(V 2 ) pairs of vertices to ask about, this takes O(V 3 ) time.

G 2 with Adjacency Lists For a given edge (x, z), we can run through all the edges from z in O(V) time, and fill the results into an adjacency matrix, which is initially empty. It takes O(VE) to construct the edges, and O(V 2 ) to initialize and read the adjacency matrix, for a total of O((V+E)V). Since E+1  V (unless the graph is disconnected), this is usually simplified to O(VE), and is faster than the previous algorithm on sparse graphs.

Graphs Motivation and Terminology Representations Traversals Variety of Problems

Traversing a Graph One of the most fundamental graph problems is to traverse every edge and vertex in a graph. Applications include:  Printing out the contents of each edge and vertex.  Counting the number of edges.  Identifying connected components of a graph. For correctness, we must do the traversal in a systematic way so that we don't miss anything. For efficiency, we must make sure we visit each edge at most twice.

Marking Vertices The idea in graph traversal is that we mark each vertex when we first visit it, and keep track of what is not yet completely explored. For each vertex, we maintain two flags:  discovered - have we encountered this vertex before?  explored - have we finished exploring this vertex? We must maintain a structure containing all the vertices we have discovered but not yet completely explored. Initially, only a single start vertex is set to be discovered.

Correctness of Graph Traversal Every edge and vertex in the connected component is eventually visited. Suppose not, i.e. there exists a vertex which was unvisited whose neighbor was visited. This neighbor will eventually be explored so we would visit it….

Traversal Orders The order we explore the vertices depends upon the data structure used to hold the discovered vertices yet to be fully explored:  Queue - by storing the vertices in a first-in, first out (FIFO) queue, we explore the oldest unexplored vertices first. Thus we radiate out slowly from the starting vertex, defining a so-called breadth-first search.  Stack - by storing the vertices in a last-in, first-out (LIFO) stack, we explore the vertices by lurching along a path, constantly visiting a new neighbor if one is available, and backing up only when surrounded by previously discovered vertices. Thus our explorations speed away from our starting point, defining a so-called depth-first search.

Example Graph A D B L K J G H I F C E

Breadth-First Search Tree A D B L K J G H I F C E

Key BFS Tree Property A D B L K J G H I F C E A D B L K J G HI F C E Every root-leaf path in a BFS tree has the fewest number of edges possible.

Questions about BFS tree Suppose we are only given a BFS tree. Can we infer information about other potential edges? Edge (B,C)? Edge (C,D)? Edge (C,H)? A D B L K J G HI F C E Every root-leaf path in a BFS tree has the fewest number of edges possible.

Depth-First Search Tree A D B L K J G H I F C E Key Property: each edge is either in DFS tree (tree edge) or leads back to an ancestor in the tree (back edge).

Directed Graph DFS trees Things are a bit more complicated –Forward edges (ancestor to descendant) –Cross Edges Not from an ancestor to a descendant Parenthesis structure still holds –The discovery and finishing times of nodes in a DFS tree have a parenthesis structure Can create a forest of DFS trees

Graphs Motivation and Terminology Representations Traversals Variety of Problems

Question Give an efficient algorithm to determine if a graph is bipartite. (Bipartite means that the graph can be colored with 2 colors such that all edges connect vertices of different colors. )

Question Give an O(V) algorithm to determine if an undirected graph contains a cycle. Note: Traversal by BFS and DFS both take O(V+E) time.

Topological Sorting A directed acyclic graph (DAG) is a directed graph with no directed cycles. A topological sort is an ordering of vertices such that all edges go from left to right. A FED BC AFEDBC

Toplogical Sorting How can BFS or DFS be useful in topological sorting of a directed graph? Key observation: A directed graph is acyclic iff DFS yields no back edges.