Download presentation

Presentation is loading. Please wait.

Published byBelinda Morrison Modified over 3 years ago

1
GRAPHS Trees Without Rules

2
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

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

4
Airline Routes Graph

5
Course Prerequisites Graph

6
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

7
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

8
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; };

9
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

10
Circular Reference nodeT arcT

11
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 };

12
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; };

13
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

14
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?

15
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); }

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

17
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

18
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); }

19
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?

Similar presentations

OK

Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.

Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on various properties of air Ppt on computer tips and tricks Ppt on equity market in india Ppt on high voltage engineering holland Ppt on health and medicine class 10 download Ppt on our changing earth for class 7 Ppt on phonetic transcription symbols Download ppt on cell structure and function for class 8 Ppt on itc group of hotels Ppt on dining etiquette