Graphs.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs CS3240, L. grewe.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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,
COSC 2007 Data Structures II Chapter 14 Graphs III.
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.
 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.
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.
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.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 20.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
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
Minimum Spanning Trees and Shortest Paths
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
Common final examinations
UCS 406 – Data Structures & Algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Yan Shi CS/SE 2630 Lecture Notes
Graphs Part 2 Adjacency Matrix
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 373: Data Structures and Algorithms
Graphs Chapter 7 Visit for more Learning Resources.
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graphs

Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning tree algorithms Shortest-path algorithm

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) V(G) and E(G) represent the sets of vertices and edges of G, respectively

Examples for Graph 1 2 1 2 1 3 3 4 5 6 G1 2 G2 G3 complete graph 1 2 1 2 1 3 3 4 5 6 G1 2 G2 complete graph incomplete graph G3 V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={(0,1),(1,0),(1,2)}

Complete Graph A complete graph is a graph that has the maximum number of edges for undirected graph with n vertices, the maximum number of edges is n(n-1)/2 for directed graph with n vertices, the maximum number of edges is n(n-1) example: G1 is a complete graph

Directed vs. undirected graphs When the edges in a graph have no direction, the graph is called undirected The pairs of vertices representing any edges is unordered e.g., (v0, v1) and (v1, v0) represent the same edge (v0, v1) = (v1,v0)

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

Trees vs graphs Trees are special cases of 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

Graph terminology (cont.) 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

Data Structures for Graphs an Adjacency Matrix A two-dimensional matrix or array that has one row and one column for each node in the graph For each edge of the graph (Vi, Vj), the location of the matrix at row i and column j is 1 All other locations are 0 For an undirected graph, the matrix will be symmetric along the diagonal For a weighted graph, the adjacency matrix would have the weight for edges in the graph

Adjacency Matrix Example 1

Adjacency Matrix Example 2

Data Structures for Graphs An Adjacency List A list of pointers, one for each node of the graph These pointers are the start of a linked list of nodes that can be reached by one edge of the graph For a weighted graph, this list would also include the weight for each edge

Adjacency List Example 1

Adjacency List Example 2

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 Good for dense graphs Memory requirements: O(V2 ) Connectivity between two vertices can be tested quickly Adjacency list Good for sparse graphs Memory requirements: O(V + E)=O(V) Vertices adjacent to another vertex can be found quickly

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

Depth-First-Search (DFS) (cont.) depth-first traversal starting at node A: ABFHCDGIE

Depth-First-Search (DFS) (cont.) Pick a starting point— say A. Then do 3 things: visit this vertex, push it onto a stack so you can remember it, and mark it so you won’t visit it again. Next, go to any vertex adjacent to A that hasn’t yet been visited. say B. Visit B, mark it, and push it on the stack. At B, repeat as before: this process is Rule 1. RULE 1 If possible, visit an adjacent unvisited vertex, mark it, and push it on the stack. RULE 2 If you can’t follow Rule 1, then, if possible, pop a vertex off the stack. RULE 3 If you can’t follow Rule 1 or Rule 2, you’re done.

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)

Breadth-First-Searching (BFS) (cont.) ABCDEFGHI

Breadth-First-Searching (BFS) (cont.) Pick a starting point— say A. RULE 1 Visit the next unvisited vertex (if there is one) that’s adjacent to the current vertex, mark it, and insert it into the queue. RULE 2 If you can’t carry out Rule 1 because there are no more unvisited vertices, remove a vertex from the queue (if possible) and make it the current vertex. RULE 3 If you can’t carry out Rule 2 because the queue is empty, you’re done

Breadth-First-Searching (BFS) (cont.) 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 ? IF(!found) Write "Path does not exist"  

Breadth-First Traversal From the starting node, follow all paths of length one Then follow paths of length two that go to unvisited nodes Continue increasing the length of the paths until there are no unvisited nodes along any of the paths

DFS and BFS - EXAMPLE depth first search: v0, v1, v3, v7, v4, v5, v2, v6 breadth first search: v0, v1, v2, v3, v4, v5, v6, v7

Depth-First Traversal Example Consider the following graph: The order of the depth-first traversal of this graph starting at node 1 would be: ??

Breadth-First Traversal Example Consider the following graph: The order of the breadth-first traversal of this graph starting at node 1 would be: ??

Traversal Analysis If the graph is connected, these methods will visit each node exactly once Because the most complex work is done when the node is visited, we can consider these to be O(N) If we count the number of edges considered, we will find that is also linear with respect to the number of graph edges

Minimum Spanning Tree (MST) A graph with the minimum number of edges necessary to connect the vertices. Figure a) shows 5 vertices with excessive number of edges, while b) shows same vertices with minimum number of edges necessary to connect them. Figure b) constitutes a minimum spanning tree (MST). There are many possible minimum spanning trees for a given set of vertices. Figure b) shows edges {AB, BC, CD, DE}, but edges {AC, CE, ED, DB} form an MST as well.

Minimum Spanning Tree (MST) The number of edges E in an MST is always one less than the number of vertices V: E = V – 1 Remember we’re not trying to find a minimum physical length, just the minimum number of edges. The algorithm for creating the minimum spanning tree is almost identical to the depth-first search or the breadth first search algorithms.

Minimum Spanning Tree with Weighted Graphs The minimum spanning tree (MST) of a weighted connected graph is a subgraph that contains all of the nodes of the original graph and a subset of the edges so that: The subgraph is connected The total weights of the subgraph edges is the smallest possible

The Dijkstra-Prim Method This is a greedy algorithm that solves the larger problem by looking at a subset and making the best choice for it In this case, we will look at all of the edges from the starting node and choose the smallest On each pass, we will choose the smallest of the edges from nodes already in the MST to nodes not in the MST (the fringe)

Dijkstra-Prim Example

Dijkstra-Prim Example

Dijkstra-Prim Example

Dijkstra-Prim Example

The Dijkstra-Prim Algorithm Select a starting node build the initial fringe from nodes connected to the starting node while there are nodes left do choose the edge to the fringe of the smallest weight add the associated node to the tree update the fringe by: adding nodes to the fringe connected to the new node updating the edges to the fringe so that they are the smallest end while

Shortest-Path Algorithm The shortest-path algorithm finds the series of edges between two nodes that has the smallest total weight

MST vs. Shortest Path The MST algorithm can skip an edge of larger weight but include many edges of smaller weight that results in a longer path than the single edge

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 Common algorithms: Dijkstra's algorithm, Bellman-Ford algorithm BFS can be used to solve the shortest path problem when the graph is weightless or all the weights are the same

Dijkstra’s Method This is similar to the Dijkstra-Prim MST algorithm, but instead of just looking at the single shortest edge from a node to the fringe, we look at the shortest path from the start node to a fringe node

Dijkstra’s Algorithm  Select a starting node  build the initial fringe from nodes connected to the starting node  while we are not at the destination node do choose the fringe node with the shortest path to the starting node add that node and its edge to the tree update the fringe by: adding nodes that are connected to the new node to the fringe for each node in the fringe do update its edge to the one connected to the tree on the shortest path to the starting node end for end while

Dijkstra’s Shortest-Path Example source destination

Dijkstra’s Shortest-Path Example

Dijkstra’s Shortest-Path Example

Dijkstra’s Shortest-Path Example

Work out the MST from A Example: Suppose we want to install a cable television line that connects six towns. Figure shows a weighted graph with six vertices, representing the towns. Each edge has a weight, representing the cost, in millions of dollars, of installing a cable link between two cities. Five links will connect the six cities, but which five links should they be?

Work out minimum fares to all locations from A This time, we just want to find the cheapest route from one city to another. Note that the edges are directed

The Shortest-Path Array A B C D E - 50(A) 100(D) 80(A) 140(C)

Efficiency The issue is complicated by the two ways of representing graphs: the adjacency matrix and adjacency lists. If an adjacency matrix is used, the algorithms mostly require O(V2) time, where V is the number of vertices. For large matrices, O(V2) isn’t very good performance. If the graph is dense, there isn’t much we can do about improving this performance. (By dense we mean a graph that has many edges—one in which many or most of the cells in the adjacency matrix are filled.) However, many graphs are sparse. In a sparse graph, running times can be improved by using the adjacency-list representation rather than the adjacency matrix. - You don’t waste time examining adjacency-matrix cells that don’t hold edges. For unweighted graphs, depth-first search with adjacency lists requires O(V+E) time For weighted graphs, both the minimum spanning tree and the shortest-path algorithm require O((E+V)logV) time. In large sparse graphs, these times can represent dramatic improvements over the adjacency matrix approach. However, the algorithms are somewhat more complicated