1 / 95

Graphs & Networks

Graphs & Networks. Briana B. Morrison Adapted from Alan Eugenio, and William J. Collins. Topics. Definitions Storage Matrix Adjacency sets Algorithms Breadth first traversal Depth first traversal Spanning trees Shortest path Implementation. A graph is a pair ( V, E ) , where

garran
Télécharger la présentation

Graphs & Networks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs & Networks Briana B. Morrison Adapted from Alan Eugenio, and William J. Collins

  2. Topics • Definitions • Storage • Matrix • Adjacency sets • Algorithms • Breadth first traversal • Depth first traversal • Spanning trees • Shortest path • Implementation Graphs

  3. A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs of vertices, called edges Vertices and edges are positions and store elements Example: A vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the mileage of the route Graph 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA Graphs

  4. V = { S, T, X, Y, Z} E = { (X, Y), (X, Z), (Y, T), (T, Z), (T, S), (S, Z)} Graphs

  5. EXAMPLE: X Y Z T 5 VERTICES 6 EDGES S Graphs

  6. Edge Types • Directed edge • ordered pair of vertices (u,v) • first vertex u is the origin • second vertex v is the destination • e.g., a flight • Undirected edge • unordered pair of vertices (u,v) • e.g., a flight route • Directed graph • all the edges are directed • e.g., route network • Undirected graph • all the edges are undirected • e.g., flight network flight AA 1206 ORD PVD 849 miles ORD PVD Graphs

  7. Applications • Electronic circuits • Printed circuit board • Integrated circuit • Transportation networks • Highway network • Flight network • Computer networks • Local area network • Internet • Web • Databases • Entity-relationship diagram Graphs

  8. V a b h j U d X Z c e i W g f Y Terminology • End vertices (or endpoints) of an edge • U and V are the endpoints of a • Edges incident on a vertex • a, d, and b are incident on V • Adjacent vertices • U and V are adjacent • Degree of a vertex • X has degree 5 • Parallel edges • h and i are parallel edges • Self-loop • j is a self-loop Graphs

  9. Graphs

  10. Terminology (cont.) • Path • sequence of alternating vertices and edges • begins with a vertex • ends with a vertex • each edge is preceded and followed by its endpoints • Simple path • path such that all its vertices and edges are distinct • Examples • P1=(V,b,X,h,Z) is a simple path • P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V b a P1 d U X Z P2 h c e W g f Y Graphs

  11. Terminology (cont.) • Cycle • circular sequence of alternating vertices and edges • each edge is preceded and followed by its endpoints • Simple cycle • cycle such that all its vertices and edges are distinct • Examples • C1=(V,b,X,g,Y,f,W,c,U,a,) is a simple cycle • C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Graphs

  12. Graphs

  13. Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet Graphs

  14. Graphs

  15. X Y Z X, Y, T, Z, S HAS LENGTH 4. T SHORTEST PATH FROM X to S = ? S IN GENERAL, HOW CAN THE SMALLEST PATH BETWEEN 2 VERTICES BE DETERMINED? Graphs

  16. Graphs

  17. X Y Z X, Y, T, Z, X IS A CYCLE. T Y, T, S, T, Y IS NOT A CYCLE. IS Y, Z, T, S, Z, X, Y A CYCLE? S Graphs

  18. Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet THIS UNDIRECTED GRAPH IS ACYCLIC. Graphs

  19. Graphs

  20. Graphs

  21. Graph Categories • A graph is connected if each pair of vertices have a path between them • A complete graph is a connected graph in which each pair of vertices are linked by an edge Graphs

  22. E D C B A Digraphs • A digraph is a graph whose edges are all directed • Short for “directed graph” • Applications • one-way streets • flights • task scheduling Graphs

  23. Graphs

  24. Graphs

  25. Example of Digraph Graphs

  26. Digraph Application • Scheduling: edge (a,b) means task a must be completed before b can be started ics21 ics22 ics23 ics51 ics53 ics52 ics161 ics131 ics141 ics121 ics171 The good life ics151 Graphs

  27. Graphs

  28. Graphs

  29. Graphs

  30. Connectedness of Digraph • Strongly connected if there is a path from any • vertex to any other vertex. (no dead ends) • Weakly connected if, for each pair of vertices vi and vj, there is either a path P(vi, vj) or a path P(vi,vj). Graphs

  31. IS THE FOLLOWING GRAPH CONNECTED? Graphs

  32. Graphs

  33. Graphs

  34. Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet Graphs

  35. Graphs

  36. Graphs

  37. Graphs

  38. Graphs

  39. Graphs

  40. Graphs

  41. Graph Storage Two ways to store a graph: • Adjacency Matrix • Adjacency Set / List Graphs

  42. Adjacency Matrix • An m by m matrix, called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (v,, vj). Its value is the weight of the edge, or -1 if the edge does not exist. Graphs

  43. Adjacency Matrix (cont.) • Note that for an undirected graph, the matrix is symmetrical about the diagonal line. X Z Y T S Graphs

  44. Adjacency Sets Graphs

  45. Vertices and edges are positions store elements Accessor methods aVertex() incidentEdges(v) endVertices(e) isDirected(e) origin(e) destination(e) opposite(v, e) areAdjacent(v, w) Update methods insertVertex(o) insertEdge(v, w, o) insertDirectedEdge(v, w, o) removeVertex(v) removeEdge(e) Generic methods numVertices() numEdges() vertices() edges() Main Methods of the Graph ADT Graphs

  46. Asymptotic Performance Graphs

  47. Animation • Implementation Animation Graphs

  48. Graphs

  49. Graphs

  50. Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G BFS on a graph with n vertices and m edges takes O(n + m ) time BFS can be further extended to solve other graph problems Find and report a path with the minimum number of edges between two given vertices Find a simple cycle, if there is one Breadth-First Search Graphs

More Related