Data Structures and Algorithms

Slides:



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

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs CS3240, L. grewe.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph & BFS.
Data Structures and Algorithms Graphs Graph Representations PLSD210(ii)
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
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.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 14 Graph Representations. Graph Representation – How do we represent a graph internally? – Two ways adjacency matrix list – Adjacency Matrix For.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Graphs – Part II CS 367 – Introduction to Data Structures.
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.
GRAPHS. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different implementations.
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.
COSC 2007 Data Structures II
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Breadth-First Search (BFS)
CSE 373 Data Structures and Algorithms
CS212: Data Structures and Algorithms
Thinking about Algorithms Abstractly
CSE 373 Topological Sort Graph Traversals
Data Structures, Algorithms & Complexity
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Ellen Walker CPSC 201 Data Structures Hiram College
CS120 Graphs.
Data Structures and Algorithms for Information Processing
CMSC 341 Lecture 21 Graphs (Introduction)
CSC 172 DATA STRUCTURES.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs & Graph Algorithms 2
Graphs Chapter 13.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Chapter 22: Elementary Graph Algorithms I
Graph Traversals Depth-First Traversals. Algorithms. Example.
Yan Shi CS/SE 2630 Lecture Notes
Graphs Part 2 Adjacency Matrix
Lecture 13 CSE 331 Sep 27, 2017.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Depth-First Search CSE 2011 Winter April 2019.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Depth-First Search CSE 2011 Winter April 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.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 10 Graph Algorithms
Chapter 9: Graphs Shortest Paths
Lecture 13 CSE 331 Sep 28, 2016.
Presentation transcript:

Data Structures and Algorithms Graphs Graph Representations PLSD210(ii)

Graphs - Data Structures Vertices Map to consecutive integers Store vertices in an array Edges Adjacency Matrix Booleans - TRUE - edge exists FALSE - no edge O(|V|2) space

Graphs - Data Structures Edges Adjacency Matrix O(|V|2) space Can be compacted 1 bit/entry If undirected, top half only

Graphs - Data Structures Edges Adjacency Lists For each vertex List of vertices “attached” to it For each edge 2 entries One in the list for each end O(|E|) space Better for sparse graphs Undirected representation

Graphs - Traversing Choices Depth First Depth-First / Breadth-first Use an array of flags to mark “visited” nodes

Graphs - Depth-First Graph data structure Adjacency Matrix ADT struct t_graph { int n_nodes; graph_node *nodes; int *visited; AdjMatrix am; } static int search_index = 0; void search( graph g ) { int k; for(k=0;k<g->n_nodes;k++) g->visited[k] = 0; search_index = 0; for(k=0;k<g->n_nodes;k++) { if ( !g->visited[k] ) visit( g, k ); Graph data structure Adjacency Matrix ADT Mark all nodes “not visited” Visit all the nodes attached to node 0, then ..

Graphs - Depth-First void visit( graph g, int k ) { int j; g->visited[k] = ++search_index; for(j=0;j<g->n_nodes;j++) { if ( adjacent( g->am, k, j ) ) { if ( !g->visited[j] ) visit( g, j ); } Mark the order in which this node was visited Visit all the nodes adjacent to this one

Graphs - Depth-First void visit( graph g, int k ) { int j; g->visited[k] = ++search_index; for(j=0;j<g->n_nodes;j++) { if ( adjacent( g->am, k, j ) ) { if ( !g->visited[j] ) visit( g, j ); } Mark the order in which this node was visited Visit all the nodes adjacent to this one C hack ... Should be g->visited[j] != 0 Search_index == 0 means not visited yet!

Graphs - Depth-First Adjacency List version of visit void visit( graph g, int k ) { AdjListNode al_node; g->visited[k] = ++search_index; al_node = ListHead( g->adj_list[k] ); while( n != NULL ) { j = ANodeIndex( ListItem( al_node ) ); if ( !g->visited[j] ) visit( g, j ); al_node = ListNext( al_node ); }

Graphs - Depth-First Adjacency List version of visit ListHead void visit( graph g, int k ) { AdjListNode al_node; g->visited[k] = ++search_index; al_node = ListHead( g->adj_list[k] ); while( n != NULL ) { j = ANodeIndex( ListItem( al_node ) ); if ( !g->visited[j] ) visit( g, j ); al_node = ListNext( al_node ); } Assumes a List ADT with methods ListHead ANodeIndex ListItem ListNext

Graph - Breadth-first Traversal Adjacency List Time complexity Visited set for each node Each edge visited twice Once in each adjacency list O(|V| + |E|) O(|V|2) for dense |E| ~ |V|2 graphs but O(|V|) for sparse |E| ~ |V| graphs Adjacency Lists perform better for sparse graphs

Graph - Breadth-first Traversal Breadth-first requires a FIFO queue static queue q; void search( graph g ) { q = ConsQueue( g->n_nodes ); for(k=0;k<g->n_nodes;k++) g->visited[k] = 0; search_index = 0; for(k=0;k<g->n_nodes;k++) { if ( !g->visited[k] ) visit( g, k ); } void visit( graph g, int k ) { al_node al_node; int j; AddIntToQueue( q, k ); while( !Empty( q ) ) { k = QueueHead( q ); g->visited[k] = ++search_index; ......

Graph - Breadth-first Traversal Breadth-first requires a FIFO queue void visit( graph g, int k ) { al_node al_node; int j; AddIntToQueue( q, k ); while( !Empty( q ) ) { k = QueueHead( q ); g->visited[k] = ++search_index; al_node = ListHead( g->adj_list[k]); while( al_node != NULL ) { j = ANodeIndex(al_node); if ( !g->visited[j] ) { AddIntToQueue( g, j ); g->visited[j] = -1; /* C hack, 0 = false! */ al_node = ListNext( al_node ); } Put this node on the queue