Graphs Motivation and Terminology Representations Traversals

Slides:



Advertisements
Similar presentations
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Advertisements

Graph & BFS.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
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.
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.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Representing and Using 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.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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.
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.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Introduction to Algorithms
Breadth-First Search (BFS)
Graphs A New Data Structure
Graphs Breadth First Search & Depth First Search
CS 201: Design and Analysis of Algorithms
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Thinking about Algorithms Abstractly
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
Depth-First Search.
Graphs Breadth First Search & Depth First Search
CS 3343: Analysis of Algorithms
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CSCE 411 Design and Analysis of Algorithms
Graph.
Graph Representation, DFS and BFS
Graph Search Applications
CSE 421: Introduction to Algorithms
Graphs Graph transversals.
Elementary Graph Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Chapter 11 Graphs.
Richard Anderson Autumn 2016 Lecture 5
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Important Problem Types and Fundamental Data Structures
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
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 .
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

Graphs Motivation and Terminology Representations Traversals Three 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 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. Living Room Den Bedroom Kitchen Hallway Bath Living Den Room Hall Bed Kitchen Bath Apartment Blueprint Our question might be, how can I get from the Kitchen to the bathroom while going through the fewest other rooms?

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 Scarecrow Dorothy Auntie M. Tin Man Cowardly Lion The Wicked Witch of the West Each vertex represents a person, and each edge indicates that the two people are friends.

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 Three Problems

Adjacency Matrix 1 2 3 4 5

Adjacency List 1 2 3 4 5 1 2 2 3 4 1 4 1 4 5 3 4 1 2 3 5 3 4 5

Tradeoffs Between Adjacency Lists and Adjacency Matrices Comparison Winner (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. (V2) 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. (V2) Better for most problems? lists Assume size is kept in the lists.

Graph Squaring The square of a directed graph G = (V, E) is the graph G2 = (V, E2), such that (x, y)  E2 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.

G2 with Adjacency Matrices To discover whether there is an edge (x, y) in E2, we do the following. For each possible intermediate vertex z, we check whether (x, z) and (z, y) exist in O(1); if so for any z, mark (x,y) in E2. Since there are O(V) intermediate vertices to check, and O(V2) pairs of vertices to ask about, this takes O(V3) time.

G2 with Adjacency Lists We use an adjacency matrix as temporary storage. For each edge (x, z), we run through all the edges (z,y) from z in O(V) time, updating the adjacency matrix for edge (x,y). We convert back to adjacency lists at the end. It takes O(VE) to construct the edges, and O(V2) 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. E ranges between O(V) and O(V2)

Graphs Motivation and Terminology Representations Traversals Three 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. To completely explore a vertex, we look at each edge going out of it. For each edge which goes to an undiscovered vertex, we mark it discovered and add it to the list of work to do. Note that regardless of what order we fetch the next vertex to explore, each edge is considered exactly twice, when each of its endpoints are explored.

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 constantly visiting a new neighbor if one is available; we back up only when surrounded by previously discovered vertices. This defines a so-called depth-first search.

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

Breadth-First Search Tree G J B H E K C What are the properties of these trees? For BFS, the tree is always the shortest distance from the starting point to any of the vertices. I A F L

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

Questions about BFS tree D B L K J G H I F C E 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)? Every root-leaf path in a BFS tree has the fewest number of edges possible.

Depth-First Search Tree G J B H E K C I A F L 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 Three Problems

Bipartite Graph? 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. )

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

Topological Sorting? A F E D B C A F E D B C A directed acyclic graph (DAG) is a directed graph with no directed cycles. A topological sort is an ordering of nodes where all edges go from left to right. How can BFS or DFS help us topologically sort a directed graph (or determine the graph is not a DAG)? A F E D B C A F E D B C