GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
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.
DATA STRUCTURE RECAP Yiqun Zhang University of Houston.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
Graphs - II CS 2110, Spring Where did I leave that book?
Graph Searching CSE 373 Data Structures Lecture 20.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Chapter 8, Part I Graph Algorithms.
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.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
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.
Advanced Data Structures
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.
Using Search in Problem Solving
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.
COMP171 Depth-First Search.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
Representing and Using Graphs
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
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.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
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.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Breadth-first and depth-first traversal CS1114
Lecture 9: Graphs & Graph Models. Definition of a Graph edge vertex cycle path.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CS 367 Introduction to Data Structures Lecture 13.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
SEARCHING GRAPHS Chapter INTRO  in our tree discussion, learned three ways of traversing a graph: –Preorder (prefix) –Inorder (infix) –Postorder.
Graphs A New Data Structure
Data Structures Graphs - Terminology
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 9: Graphs Basic Concepts
Graphs Part 2 Adjacency Matrix
Spanning Trees Longin Jan Latecki Temple University based on slides by
Algorithms: Design and Analysis
Chapter 9: Graphs Basic Concepts
Data Structures Using C++ 2E
Presentation transcript:

GRAPHS Trees Without Rules

Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each other

Facebook Graph June Sarah Susan Robert Judy Katie Lila Rebecca John Vertices: people Edges: friends

Airline Routes Graph

Course Prerequisites Graph

Graphs  Another recursive data structure  Node with any number of pointers to other nodes  No restrictions on connectivity Allows disconnected nodes, multiple paths, and cycles  Terminology  Node  Arc  Directed  Connected  Path  Cycle

Implementation Strategies  Create a set of all arcs  (a->b, a->c, b->d, c->d, d->a, d->b)  Adjacency List  a: {b, c}  b: {d}  c:{d}  d:{a, b}  Adjacency matrix  Consider the data  Sparse vs. Dense  Space vs. Time a cb d

Coding Graphs  Graph itself is set or vector of node *  Why not just pointer to a root, like a tree?  Could you designate an arbitrary node as root? Struct nodeT { //data for node goes here vector connected; };

Coding Graphs  Often graphs have data associated with the arc itself.  Unlike trees and lists where links are only for connecting nodes  Arcs may have information like distance or cost. Add struct to hold arc info  Arc has pointers to start/end node and a node has a collection of arcs Circular reference

Circular Reference nodeT arcT

Arcs have Nodes and Nodes have Arcs struct arcT { //arc fields go here nodeT *start, *end; //error! nodeT not defined }; struct nodeT { //node fields go here vector outgoing; //arcT already defined };

Making the Gears Work struct nodeT; //forward reference struct arcT { //arc fields go here nodeT *start, *end; }; struct nodeT { //node fields go here vector outgoing; };

Graph Traversals  Traverse reachable nodes  Start from a node and follow arcs to other nodes  Some graphs not fully connected, so not all nodes are reachable  Depth-first and Breadth-first  Similar to tree’s post-order, pre-order and in-order  Both visit all reachable nodes, but in a different order  Possibility of cycles means you must track “visited” nodes to avoid infinite loop Could maintain a visited flag per node or set of visited nodes

Depth-First Traversal  Choose a starting node  No root node in graph, may have specific start in mind or just choose one randomly  Go Deep  Pick a neighbor, explore all reachable from there  Backtrack  After fully exploring everything reachable from first neighbor, choose another neighbor and go again  Continue until all neighbors are exhausted  Base case?

Depth-First Pseudocode void DepthFirstSeach(node *cur, set & visited) { //if visited contains cur, do nothing. //otherwise, add cur to visited set //for all arcT’s in the graph vector named outgoing DepthFirstSearch(cur->outgoing[i]->end, visited); }

Trace A B C D E F G H A B C D E F G H

Breadth-First Traversal  Choose starting node  Visit all immediate neighbors  Those directly connected to start node  Branch out to all neighbors 2 hops away  Again to 3 hops and so on  Until all reachable nodes are visited  How to manage nodes to vist  Perfect for the queue  What about cycles/multiple paths?  Need to track visited status

Breadth-First Pseudocode void BreadthFirstSearch(nodeT *start) { queue q; set visited; q.enqueue(start); //while q is not empty nodeT *cur = q.dequeue(); //if visited does not contain cur, add cur to visited and //for all arcTs in vector named outgoing... q.enqueue(cur->outgoing[i]->end); }

Graph Search Algorithms  Many interesting questions are really just graph searches  Which nodes are reachable from this node?  Does the graph have a cycle?  Is the graph fully connected?  Longest path without a cycle?  Is there a continuous path that visits all nodes once and exactly once?