Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

CS 253: Algorithms Chapter 22 Graphs Credit: Dr. George Bebis.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
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.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
Eulerian Graphs CSE 331 Section 2 James Daly. Reminders Project 3 is out Covers graphs Due Friday.
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.
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.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
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.
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 Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
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.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Graphs Lecture 19 CS2110 – Spring 2013.
CSE 373 Topological Sort Graph Traversals
CSE 2331/5331 Topic 9: Basic Graph Alg.
Depth-First Search.
CS120 Graphs.
Graph.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
CS223 Advanced Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Representation (23.1/22.1)
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
Richard Anderson Autumn 2016 Lecture 5
CSCI2100 Data Structures Tutorial
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Graphs G = (V, E) V are the vertices; E are the edges.
CE 221 Data Structures and Algorithms
Elementary Graph Algorithms
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Winter 2019 Lecture 5
Heaps Chapter 6 Section 6.9.
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

Graphs CSE 331 Section 2 James Daly

Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due next week Friday

Graphs G = (V, E) G is a graph V is a set of vertices / nodes E is a set of edges between vertices A vertex u is adjacent to vertex v if and only if the edge uv is in E

Graph Example V2V2 V1V1 V3V3 V5V5 V4V4 V1 is connected to V3 but not to V4

Digraphs In directed graphs, edges go one way only Directed edges are usually called arcs V2V2 V1V1 V3V3 V5V5 V4V4 V3 is adjacent to V1 But V1 is not adjacent to V3

Weighted Graphs Edges or arcs may have weights V2V2 V1V1 V3V3 V5V5 V4V

Paths A path is a sequence of vertices v 1, v 2, …, v n such that v i v i+1 is in E for 1 ≤ i < n A simple path is a path where all vertices are distinct (except maybe the first and last) A cycle is a path that starts and ends at the same vertex

Directed Acyclic Graphs (DAGs) A DAG is a digraph with no directed cycles All rooted trees are DAGs Is there only ever one path between nodes?

Representing Graphs V = {a, b, c, d, e, f, g}|V| E = {(a, b), (a, e), (a, g), (b, c), (b, d), (c, d), (c, e), (d, f), (e, g), (f, e), (f, g)}|E| Space: O(|V| + |E|) a b c d f e g

Adjacency List V = {a, b, …, g} a: b: c: … Space: O(|V| + |E|) Good for sparse graphs (few edges) a b c d f e g begacdbde

Adjacency Matrix a b c d f e g a b c d e f g abcdefgabcdefg Good for dense graphs (lots of edges)

Degree The degree of a vertex v is the number of edges connected to v In a digraph The out-degree is the number of arcs leaving v The in-degree is the number of arcs entering v

Handshaking Lemma = 4 = 2 * 2

Searching / Traversal Two main methods Depth-first search (DFS) Go as far as possible, then backtrack Uses stacks Breadth-first search (BFS) Check neighborhood first and then spread out Uses queues

DFS(v) V1V1 V2V2 V3V3 V4V4 V5V5 DFS(v1):v1v2v4v3v5 V1V2V4V3V5

DFS(v) [Non-recursive]

BFS(v)

BFS V1V1 V2V2 V3V3 V4V4 V5V5 BFS(v1): Distance: v1 0 v2 1 v4 2 v3 1 v5 2 V1V2 V3 V4V5

Topological Sort Ordering vertices in a DAG such that if there is a path from u to v then u appears before v in the ordering CSE Courses: What order can you take classes to meet your prerequisites?

TopSort(G) Q ← () // Empty queue ForEach v in V: If InDegree(v) = 0 then Q.Enqueue(v) While Q not empty: v ← Q.Dequeue() Process(v) ForEach u in Neighbors(v): InDegree(u)-- If InDegree(u) = 0 Then Q.Enqueue(v)