ece 242 data structures
play

ECE 242 Data Structures Lecture 28 Introduction to Graphs - PDF document

ECE 242 Data Structures Lecture 28 Introduction to Graphs November 20, 2009 ECE242 L28: Introduction to Graphs Overview Problem: How do we represent irregular connections between locations? Graphs Definition Directed and


  1. ECE 242 Data Structures Lecture 28 Introduction to Graphs November 20, 2009 ECE242 L28: Introduction to Graphs Overview ° Problem: How do we represent irregular connections between locations? ° Graphs • Definition • Directed and Undirected graph • Simple path and cycle • Connected and Unconnected graph • Weighted graph ° Graph Representation ° Graph Traversal November 20, 2009 ECE242 L28: Introduction to Graphs

  2. Abstract Data Type ° We have discussed: • List • Tree • Today we will talk about Graphs November 20, 2009 ECE242 L28: Introduction to Graphs Northwest Airline Flight Plan Anchorage Boston Minneapolis Seattle Hartford SF Atlanta Austin November 20, 2009 ECE242 L28: Introduction to Graphs

  3. Computer Network Or Internet AT&T MCI Regional Network Campus Intel Umass November 20, 2009 ECE242 L28: Introduction to Graphs Lots of Interesting Problems ° Graph traversal ° Shortest path between two nodes ° Anything else? November 20, 2009 ECE242 L28: Introduction to Graphs

  4. Concepts of Graphs node or vertex edges (weight) November 20, 2009 ECE242 L28: Introduction to Graphs Graph Definition ° G = (V, E) ° V is the vertex set • Vertices are also called nodes or points ° E is the edge set • Each edge connects two vertices • Edges are also called arcs or lines November 20, 2009 ECE242 L28: Introduction to Graphs

  5. Graphs, Vertices and Edges November 20, 2009 ECE242 L28: Introduction to Graphs Undirected vs. Directed Graph Undirected Graph Directed Graph – no oriented edge – every edge has oriented vertex November 20, 2009 ECE242 L28: Introduction to Graphs

  6. Subgraph ° Subgraph: • subset of vertices and edges November 20, 2009 ECE242 L28: Introduction to Graphs Simple Path ° A simple path traverse a node no more than once • ABCD is a simple path B C A path D November 20, 2009 ECE242 L28: Introduction to Graphs

  7. Cycle ° A cycle is a path that starts and ends at the same point • CBDC is a cycle B C A D November 20, 2009 ECE242 L28: Introduction to Graphs Connected vs. Unconnected Graph Connected Graph Unconnected Graph November 20, 2009 ECE242 L28: Introduction to Graphs

  8. Directed Acyclic Graph ° Directed Acyclic Graph (DAG) : directed graph without cycle ° Examples • Course Requirement Graph: DAG November 20, 2009 ECE242 L28: Introduction to Graphs Directed Acyclic Graph ° This is not DAG • ABCD is a cycle D C A B November 20, 2009 ECE242 L28: Introduction to Graphs

  9. Weighted Graph ° Weighted graph: a graph with numbers assigned to its edges ° Weight: cost, distance, travel time, hop, etc. 0 10 20 1 1 2 4 5 3 November 20, 2009 ECE242 L28: Introduction to Graphs Representation Of Graph ° Two representations • Adjacency Matrix • Adjacency List November 20, 2009 ECE242 L28: Introduction to Graphs

  10. Adjacency Matrix ° N nodes in graph ° Use Matrix A[0…N-1][0…N-1] • if vertex i and vertex j are adjacent in graph, A[i][j] = 1, • otherwise A[i][j] = 0 • if vertex i has a loop, A[i][i] = 1 • if vertex i has no loop, A[i][i] = 0 November 20, 2009 ECE242 L28: Introduction to Graphs Example of Adjacency Matrix A[i][j] 0 1 2 3 0 0 1 1 0 0 1 1 0 1 1 2 1 1 0 1 3 0 1 1 0 1 2 3 0 1 1 0 1 0 1 1 So, Matrix A = 1 1 0 1 0 1 1 0 November 20, 2009 ECE242 L28: Introduction to Graphs

  11. Undirected vs. Directed ° Undirected graph • adjacency matrix is symmetric • A[i][j] always equals A[j][i] ° Directed graph • adjacency matrix may not be symmetric • A[i][j] may not equal A[j][i] November 20, 2009 ECE242 L28: Introduction to Graphs Directed Graph Matrix Representation A[i][j] 0 1 2 3 0 0 1 1 1 1 0 0 0 1 2 0 0 0 1 0 3 0 0 0 0 1 2 0 1 1 1 3 0 0 0 1 So, Matrix A = 0 0 0 1 0 0 0 0 November 20, 2009 ECE242 L28: Introduction to Graphs

  12. Weighted Graph A[i][j] 0 1 2 3 0 0 20 10 1 1 20 0 0 5 2 10 0 0 4 0 10 3 1 5 4 0 20 1 2 1 0 20 10 1 4 5 20 0 0 5 3 So, Matrix A = 10 0 0 4 1 5 4 0 November 20, 2009 ECE242 L28: Introduction to Graphs Adjacency Matrices November 20, 2009 ECE242 L28: Introduction to Graphs

  13. Adjacency List ° An array of list ° the i th element of array is a list of vertices that connect to vertex i November 20, 2009 ECE242 L28: Introduction to Graphs Example of Adjacency List 0 1 2 3 0 1 3 2 3 1 2 3 3 vertex 0 connect to vertex 1, 2 and 3 vertex 1 connects to 3 vertex 2 connects to 3 November 20, 2009 ECE242 L28: Introduction to Graphs

  14. Weighted Graph ° Weighted graph: extend each node with an addition field: weight 0 1 10 2 20 3 1 0 10 20 1 0 10 3 4 1 1 2 2 0 20 3 5 4 5 3 0 1 1 4 2 5 3 November 20, 2009 ECE242 L28: Introduction to Graphs Adjacency List November 20, 2009 ECE242 L28: Introduction to Graphs

  15. Compare Two Representations ° Given two vertices: u, v • find out if u and v are adjacent ° Given a vertex: u • enumerate all neighbors of u ° For all vertices • enumerate all neighbors of each vertex November 20, 2009 ECE242 L28: Introduction to Graphs Comparison Of Representations Cost Adjacency Matrix Adjacency List Given two vertices u and v: degree of node O(1) find out whether u and v are adjacent O(N) Given a vertex u: degree of node O(N) O(N) enumerate all neighbors of u Summations of For all vertices: all node degree O(N 2 ) enumerate all neighbors of each vertex O(E) November 20, 2009 ECE242 L28: Introduction to Graphs

  16. Complete Graph • There is an edge between any two vertices Total number of edges in graph: E = N(N-1)/2 = O(N 2 ) November 20, 2009 ECE242 L28: Introduction to Graphs Sparse Graph • There are very small no. of edges in the graph For example: E = N-1= O(N) November 20, 2009 ECE242 L28: Introduction to Graphs

  17. Space Requirement of Representations ° Memory space: O(N 2 ) • adjacency matrix • adjacency list O(E) ° Sparse graph • adjacency list is better ° Dense graph • same running time November 20, 2009 ECE242 L28: Introduction to Graphs Summary ° We will spend the rest of the semester discussing graphs ° Many important problems represented as graphs • Mapquest, chip routing, Google, maze type games ° Next step: search for a node in a graph ° Other problem: shortest path to node, shortest path to all nodes November 20, 2009 ECE242 L28: Introduction to Graphs

Recommend


More recommend