Graphs CS 308 – Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

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.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 20: Graphs CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Graphs CS3240, L. grewe.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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 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 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs CS 302 – Data Structures Section 9.3. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges between the.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Search Related Algorithms. Graph Code Adjacency List Representation:
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
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.
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
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++
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
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 9 Graphs Modified from Dr George Bebis. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges between.
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:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Chapter 9 Priority Queues, Heaps, and Graphs
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
UNIT – III PART - II Graphs By B VENKATESWARLU, CSE Dept.
Data Structures 13th Week
Graphs CS Data Structures Mehmet H Gunes
Csc 2720 Instructor: Zhuojun Duan
Chapter 9 Graphs and Sets
Priority Queues, Heaps, and Graphs
UCS 406 – Data Structures & Algorithms
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
CS120 Graphs.
Graphs Representation, BFS, DFS
Graphs.
Chapter 11 Graphs.
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 10 The Graph ADT.
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 CS 308 – Data Structures

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 edges describes relationships among the vertices

Formal definition of graphs A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices)

Directed vs. undirected graphs When the edges in a graph have no direction, the graph is called undirected

When the edges in a graph have a direction, the graph is called directed (or digraph) Directed vs. undirected graphs (cont.) E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) Warning: if the graph is directed, the order of the vertices in each edge is important !!

Trees are special cases of graphs!! Trees vs graphs

Graph terminology Adjacent nodes: two nodes are adjacent if they are connected by an edge Path: a sequence of vertices that connect two nodes in a graph Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5

What is the number of edges in a complete directed graph with N vertices? N * (N-1) Graph terminology (cont.)

What is the number of edges in a complete undirected graph with N vertices? N * (N-1) / 2 Graph terminology (cont.)

Weighted graph: a graph in which each edge carries a value Graph terminology (cont.)

Graph implementation Array-based implementation –A 1D array is used to represent the vertices –A 2D array (adjacency matrix) is used to represent the edges

Array-based implementation

Graph implementation (cont.) Linked-list implementation –A 1D array is used to represent the vertices –A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)

Linked-list implementation

Adjacency matrix vs. adjacency list representation Adjacency matrix –Good for dense graphs --|E|~O(|V| 2 ) –Memory requirements: O(|V| + |E| ) = O(|V| 2 ) –Connectivity between two vertices can be tested quickly Adjacency list –Good for sparse graphs -- |E|~O(|V|) –Memory requirements: O(|V| + |E|)=O(|V|) –Vertices adjacent to another vertex can be found quickly

Graph specification based on adjacency matrix representation const int NULL_EDGE = 0; template class GraphType { public: GraphType(int); ~GraphType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void AddVertex(VertexType); void AddEdge(VertexType, VertexType, int); int WeightIs(VertexType, VertexType); void GetToVertices(VertexType, QueType &); void ClearMarks(); void MarkVertex(VertexType); bool IsMarked(VertexType) const; private: int numVertices; int maxVertices; VertexType* vertices; int **edges; bool* marks; }; (continues)

template GraphType ::GraphType(int maxV) { numVertices = 0; maxVertices = maxV; vertices = new VertexType[maxV]; edges = new int[maxV]; for(int i = 0; i < maxV; i++) edges[i] = new int[maxV]; marks = new bool[maxV]; } template GraphType ::~GraphType() { delete [] vertices; for(int i = 0; i < maxVertices; i++) delete [] edges[i]; delete [] edges; delete [] marks; } (continues)

void GraphType ::AddVertex(VertexType vertex) { vertices[numVertices] = vertex; for(int index = 0; index < numVertices; index++) { edges[numVertices][index] = NULL_EDGE; edges[index][numVertices] = NULL_EDGE; } numVertices++; } template void GraphType ::AddEdge(VertexType fromVertex, VertexType toVertex, int weight) { int row; int column; row = IndexIs(vertices, fromVertex); col = IndexIs(vertices, toVertex); edges[row][col] = weight; } (continues)

template int GraphType ::WeightIs(VertexType fromVertex, VertexType toVertex) { int row; int column; row = IndexIs(vertices, fromVertex); col = IndexIs(vertices, toVertex); return edges[row][col]; }

Graph searching Problem: find a path between two nodes of the graph (e.g., Austin and Washington) Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

Depth-First-Search (DFS) What is the idea behind DFS? –Travel as far as you can down a path –Back up as little as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) DFS can be implemented efficiently using a stack

Set found to false stack.Push(startVertex) DO stack.Pop(vertex) IF vertex == endVertex Set found to true ELSE Push all adjacent vertices onto stack WHILE !stack.IsEmpty() AND !found IF(!found) Write "Path does not exist" Depth-First-Search (DFS) (cont.)

start end (initialization)

template void DepthFirstSearch(GraphType graph, VertexType startVertex, VertexType endVertex) { StackType stack; QueType vertexQ; bool found = false; VertexType vertex; VertexType item; graph.ClearMarks(); stack.Push(startVertex); do { stack.Pop(vertex); if(vertex == endVertex) found = true; (continues)

else { if(!graph.IsMarked(vertex)) { graph.MarkVertex(vertex); graph.GetToVertices(vertex, vertexQ); while(!vertexQ.IsEmpty()) { vertexQ.Dequeue(item); if(!graph.IsMarked(item)) stack.Push(item); } } while(!stack.IsEmpty() && !found); if(!found) cout << "Path not found" << endl; } (continues)

template void GraphType ::GetToVertices(VertexType vertex, QueTye & adjvertexQ) { int fromIndex; int toIndex; fromIndex = IndexIs(vertices, vertex); for(toIndex = 0; toIndex < numVertices; toIndex++) if(edges[fromIndex][toIndex] != NULL_EDGE) adjvertexQ.Enqueue(vertices[toIndex]); }

Breadth-First-Searching (BFS) What is the idea behind BFS? –Look at all possible paths at the same depth before you go at a deeper level –Back up as far as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex)

BFS can be implemented efficiently using a queue Set found to false queue.Enqueue(startVertex) DO queue.Dequeue(vertex) IF vertex == endVertex Set found to true ELSE Enqueue all adjacent vertices onto queue WHILE !queue.IsEmpty() AND !found Should we mark a vertex when it is enqueued or when it is dequeued ? Breadth-First-Searching (BFS) (cont.) IF(!found) Write "Path does not exist"

startend (initialization)

next:

template void BreadthFirtsSearch(GraphType graph, VertexType startVertex, VertexType endVertex); { QueType queue; QueType vertexQ;// bool found = false; VertexType vertex; VertexType item; graph.ClearMarks(); queue.Enqueue(startVertex); do { queue.Dequeue(vertex); if(vertex == endVertex) found = true; (continues)

else { if(!graph.IsMarked(vertex)) { graph.MarkVertex(vertex); graph.GetToVertices(vertex, vertexQ); while(!vertxQ.IsEmpty()) { vertexQ.Dequeue(item); if(!graph.IsMarked(item)) queue.Enqueue(item); } } while (!queue.IsEmpty() && !found); if(!found) cout << "Path not found" << endl; }

Single-source shortest-path problem There are multiple paths from a source vertex to a destination vertex Shortest path: the path whose total weight (i.e., sum of edge weights) is minimum Examples: –Austin->Houston->Atlanta->Washington: 1560 miles –Austin->Dallas->Denver->Atlanta->Washington: 2980 miles

Common algorithms: Dijkstra's algorithm, Bellman-Ford algorithm weightless BFS can be used to solve the shortest graph problem when the graph is weightless or all the weights are the same (mark vertices before Enqueue) Single-source shortest-path problem (cont.)

Exercises 24-37