Graphs Representation, BFS, DFS

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Chapter 8, Part I Graph Algorithms.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
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.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
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.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graph Search Applications, Minimum Spanning Tree
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 20.
Chapter 22 Elementary Graph Algorithms
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Data Structures, Algorithms & Complexity
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
Common final examinations
Unweighted Shortest Path Neil Tang 3/11/2010
CS120 Graphs.
Comp 245 Data Structures Graphs.
CSC 172 DATA STRUCTURES.
Graph Algorithm.
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Graph & BFS.
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs.
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Graph Implementation.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 10 Graph Algorithms
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 .
Presentation transcript:

Graphs Representation, BFS, DFS

Graph representations Nodes or vertices Edges Weighted vs. unweighted Undirected vs. directed Multiple edges or not Self-loops or not

Degree Vertex degree: edges for that vertex Undirected Directed Degree is the number of edges leaving vertex Self loops count twice Number of edges = 2*sum of vertex degrees Directed In-degree (incoming edge count) and out-degree (outgoing edge count) Sum of all in-degrees equals sum of all out-degrees

Traveling a graph Path/walk/trail Cycle/circuit/tour Follow edges along vertices Could repeat edge unless stated otherwise Cycle/circuit/tour Path that leads back to the original vertex Usually assumes won’t follow same edge, but not always

Storing graphs Edge list Keep a list of all edges Store start, end, weight Good if you need to work with all the edges at once Kruskal’s MST algorithm Not good at all if you want to know the adjacent edges for any one vertex

Storing graphs Adjacency list Keep a list of edges in each vertex Can be used for directed or undirected Undirected – must keep consistent on both ends Can keep weights per edge in the node list Or, store pointer to a light edge structure Good to find and iterate through edges for any vertex Typical “default” implementation for a graph

Storing graphs Adjacency matrix Store matrix indicating edges between vertices (rows/columns) Directed: rows are from, columns are to Value in the cell can be the weight of the edge Bad if the graph is sparse, or too large Can sometimes phrase graph calculations as matrix calculations this way; then, it can be more efficient to compute with

Depth-First Search Keep global list of visited T/F (or a number indicating which “tree” it is part of) Recursive function: Mark current node visited For all adjacent edges: If the node is unvisited, visit it Basically, keep a stack of nodes that you want to visit Forms the basis for several other operations

Breadth-first search Keep a “distance” for each node, initialized to infinity Keep queue of nodes to process, start with intro node Repeatedly pop a node off the queue Go through list of adjacent nodes If node is infinity away, then set its distance to current distance plus one and add to queue Has a few good uses Shortest path in an undirected graph