16-Graphs Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
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.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
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.
Shortest Path Problems
Fonts: MTExtra:  (comment) Symbol:  Wingdings:  Graphs 6-Graphs.
1 Data Structures DFS, Topological Sort Dana Shapira.
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)
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
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.
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.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
IS 2610: Data Structures Graph April 5, 2004.
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.
Foundations of Discrete Mathematics
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
COSC 2007 Data Structures II Chapter 14 Graphs I.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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++
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.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
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.
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
12. Graphs and Trees 2 Summary
Graphs Breadth First Search & Depth First Search
CS120 Graphs.
Graph Algorithm.
Finding Shortest Paths
Graphs Chapter 11 Objectives Upon completion you will be able to:
Elementary Graph Algorithms
Presentation transcript:

16-Graphs Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:

2 6-Graphs What’s a Graph? A bunch of vertices connected by edges vertex edge

3 6-Graphs Why Graph Algorithms? They’re fun. They’re interesting. They have surprisingly many applications.

4 6-Graphs Graphs are Everywhere

5 6-Graphs Adjacency as a Graph Each vertex represents a state, country, etc. There is an edge between two vertices if the corresponding areas share a border. WYNE COKS

6 6-Graphs When a Graph? Graphs are a good representation for any collection of objects and binary relation among them: - The relationship in space of places or objects - The ordering in time of events or activities - Family relationships - Taxonomy (e.g. animal - mammal - dog) - Precedence (x must come before y) - Conflict (x conflicts or is incompatible with y) - Etc.

7 6-Graphs Our Menu Depth-First Search Connected components Cycle detection Topological sort Minimal Spanning Tree Kruskal’s Prim’s Single-Source Shortest Paths Dijkstra’s Bellman-Ford DAG-SSSP All-Pairs Shortest Paths Floyd-Warshall Johnson’s

8 6-Graphs Basic Concepts A graph is an ordered pair (V, E). V is the set of vertices. (You can think of them as integers 1, 2, …, n.) E is the set of edges. An edge is a pair of vertices: (u, v). Note: since E is a set, there is at most one edge between two vertices. (Hypergraphs permit multiple edges.) Edges can be labeled with a weight: 10

9 6-Graphs Concepts: Directedness In a directed graph, the edges are “one-way.” So an edge (u, v) means you can go from u to v, but not vice versa. In an undirected graph, there is no direction on the edges: you can go either way. (Also, no self-loops.) a self-loop

10 6-Graphs Concepts: Adjacency Two vertices are adjacent if there is an edge between them. For a directed graph, u is adjacent to v iff there is an edge (v, u). uw v u is adjacent to v. v is adjacent to u and w. w is adjacent to v. uw v u is adjacent to v. v is adjacent to w.

11 6-Graphs Concepts: Degree Undirected graph: The degree of a vertex is the number of edges touching it. For a directed graph, the in-degree is the number of edges entering the vertex, and the out-degree is the number leaving it. The degree is the in-degree + the out-degree. degree 4 in-degree 2, out-degree 1

12 6-Graphs Concepts: Path A path is a sequence of adjacent vertices. The length of a path is the number of edges it contains, i.e. one less than the number of vertices. We write u  v if there is path from u to v. (The correct symbol, a wiggly arrow, is not available in standard fonts.) We say v is reachable from u Is there a path from 1 to 4? What is its length? What about from 4 to 1? How many paths are there from 2 to 3? From 2 to 2? From 1 to 1?

13 6-Graphs Concepts: Cycle A cycle is a path of length at least 1 from a vertex to itself. A graph with no cycles is acyclic. A path with no cycles is a simple path. The path is a cycle

14 6-Graphs Concepts: Connectedness An undirected graph is connected iff there is a path between any two vertices. The adjacency graph of U.S. states has three connected components. Name them. (We say a directed graph is strongly connected iff there is a path between any two vertices.) An unconnected graph with three connected components.

15 6-Graphs Concepts: Trees A free tree is a connected, acyclic, undirected graph. To get a rooted tree (the kind we’ve used up until now), designate some vertex as the root. If the graph is disconnected, it’s a forest. Facts about free trees: |E| = |V| -1 Any two vertices are connected by exactly one path. Removing an edge disconnects the graph. Adding an edge results in a cycle.

16 6-Graphs Graph Size We describe the time and space complexity of graph algorithms in terms of the number of vertices, |V|, and the number of edges, |E|. |E| can range from 0 (a totally disconnected graph) to |V| 2 (a directed graph with every possible edge, including self-loops). Because the vertical bars get in the way, we drop them most of the time. E.g. we write  (V + E) instead of  (|V| + |E|).

17 6-Graphs Representing Graphs Adjacency matrix: if there is an edge from vertex i to j, a ij = 1; else, a ij = 0. Space:  (V 2 ) Adjacency list: Adj[v] lists the vertices adjacent to v. Space:  (V+E) Adj: Represent an undirected graph by a directed one:

18 6-Graphs Depth-First Search A way to “explore” a graph. Useful in several algorithms. Remember preorder traversal of a binary tree? Binary-Preorder(x): 1number x 2 Binary-Preorder(left[x]) 3Binary-Preorder(right[x]) Can easily be generalized to trees whose nodes have any number of children. This is the basis of depth-first search. We “go deep.”

19 6-Graphs DFS on Graphs The wrong way: Bad-DFS(u) 1number u 2for each v in Adj[u] do 3Bad-DFS(v) What’s the problem?

20 6-Graphs Fixing Bad-DFS We’ve got to indicate when a node has been visited. Following CLRS, we’ll use a color: WHITEnever seen GRAYdiscovered but not finished (still exploring its descendants) BLACKfinished

21 6-Graphs A Better DFS  initially, all vertices are WHITE Better-DFS(u) color[u]  GRAY number u with a “discovery time” for each v in Adj[u] do if color[v] = WHITE then  avoid looping! Better-DFS(v) color[u]  BLACK number u with a “finishing time”

22 6-Graphs Depth-First Spanning Tree As we’ll see, DFS creates a tree as it explores the graph. Let’s keep track of the tree as follows (actually it creates a forest not a tree): When v is explored directly from u, we will make u the parent of v, by setting the predecessor, aka, parent (  ) field of v to u: u v u v  [v]  u

23 6-Graphs Two More Ideas 1. We will number each vertex with discovery and finishing times—these will be useful later. The “time” is just a unique, increasing number. The book calls these fields d[u] and f[u]. 2. The recursive routine we’ve written will only explore a connected component. We will wrap it in another routine to make sure we explore the entire graph.

24 6-Graphs

25 6-Graphs

26 6-Graphs graphs from p. 1081