1 / 61

Part 10. Graphs

Part 10. Graphs. CS 200 Algorithms and Data Structures. Outline. Introduction Terminology Shortest Paths Implementing Graphs Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits. Graphs. What can this represent? A computer network

etenia
Télécharger la présentation

Part 10. Graphs

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. Part 10. Graphs CS 200 Algorithms and Data Structures

  2. Outline • Introduction • Terminology • Shortest Paths • Implementing Graphs • Graph Traversals • Topological Sorting • Spanning Trees • Minimum Spanning Trees • Circuits

  3. Graphs What can this represent? • A computer network • Abstraction of a map • Social network A collection of nodes and edges

  4. Directed Graphs A collection of nodes and directed edges Sometimes we want to represent directionality: • Unidirectional network connections • One way streets • The web

  5. Example • “Follow the money” example (the international science and engineering visualization challenge, 2009, Science magazine and National Science Foundation) • http://www.sciencemag.org/site/special/vis2009/show/ • Reference The Scaling Laws of Human Travel, D. Brockmann, L. Hufnagel and T. Geisel, Nature 439, 462 (2006) • http://rocs.northwestern.edu/index_assets/brockmann2006nature.pdf

  6. Outline • Introduction • Terminology • Implementing Graphs • Shortest Paths • Properties • Graph Traversals • Topological Sorting • Spanning Trees • Minimum Spanning Trees • Circuits

  7. Edges Vertices/ Nodes Graph Terminology G=(V, E) Two vertices are adjacent if they are connected by an edge. An edge is incident on two vertices Degree of a vertex: number of edges incident on it Vertices Edges u e v Graph terminology: 14.1 in W&M, 9.1 in Rosen

  8. subgraph Graph Terminology A subgraphof a graph G = (V,E) is a graph (V’,E’) such that V’ is a subset of V and, E’ is a subset of E

  9. Paths • Path: a sequence of edges • (e1, e2, e3) is a path of length 3 from v1 to v4 • In a simple graph a path can be represented as a sequence of vertices v1 e1 v2 v4 e2 e3 v3

  10. Graph Terminology Self loop (loop): an edge that connects a vertex to itself Simple graph: no self loops and no two edges connect the same vertices Multigraph: may have multiple edges connecting the same vertices Pseudograph: multigraph with self-loops

  11. Directed Graphs Indegree: number of incoming edges Outdegree: number of outgoing edges w v

  12. a b c d e f g Connected Components • An undirected graph is called connected if there is a path between every pair of vertices of the graph. • A connected component of a graph G is a connected subgraph of G that is not a proper subgraph of another connected subgraph of G. G={{a,b,c,d,e,f,g},E} G1={{a,b,c},E1} G2={{d,e,f,g}, E2}

  13. The degree of a vertex • The degree of a vertex in an undirected graph • the number of edges incident with it • except that a loop at a vertex contributes twice to the degree of that vertex.

  14. Example d c b a g e f deg(a) = 2 deg(b) = deg(f) = 4 deg(d) = 1 deg(e) = 3 deg(g) = 0

  15. Complete Graphs • Simple graph that contains exactly one edge between each pair of distinct vertices. Complete Graph

  16. Cycles The cycle Cn, n≥ 3, consists of n vertices v1, v2, …, vnand edges {v1, v2}, {v2, v3},…., and {vn-1, vn}.

  17. Wheels • We obtain the wheel Wn when we add an additional vertex to the cycle Cn, for n≤ 3, and connect this new vertex to each of the nvertices in Cn, by new edges

  18. n-Cubes (n-dimensional hypercube) Hypercube 110 111 101 100 001 010 011 000

  19. Bipartite Graphs • A simple graph G is called bipartite, if its vertex set V can be partitioned into two disjoint sets V1 and V2 such that every edge in the graph connects a vertex in V1 and a vertex in V2.

  20. Outline • Introduction • Terminology • Implementing Graphs • Shortest Paths • Properties • Graph Traversals • Topological Sorting • Spanning Trees • Minimum Spanning Trees • Circuits

  21. Shortest Path Algorithms (Dijkstra’s Algorithm) • Graph G(V,E) with non-negative weights (“distances”) • Compute shortest distances from vertex s to every other vertex in the graph

  22. Shortest Path Algorithms (Dijkstra’s Algorithm) • Algorithm • Maintain array d (minimum distance estimates) • Init: d[s]=0, d[v]=vV-s • Priority queue of vertices not yet visited • select minimum distance vertex, visit v, update neighbors

  23. Shortest Path Algorithms (Dijkstra’s Algorithm) 1 e b 10 3 2 9 a 4 6 5 7 c d 2

  24. Shortest Path Algorithms (Dijkstra’s Algorithm): Initialize a bcde 0 ∞ ∞ ∞ ∞ e b 8 1 8 10 3 2 9 0 a 4 6 5 7 8 8 2 d c

  25. Shortest Path Algorithms (Dijkstra’s Algorithm): step 1 a bcde 0 10/a 5/a -- -- a bcde 0 10/a 5/a -- -- e b 1 8 10 a bcde 0 ∞ ∞ ∞ ∞ 10 3 2 9 0 a 4 6 5 7 5 8 2 d c

  26. Shortest Path Algorithms (Dijkstra’s Algorithm): step 2 a bcde 0 10/a 5/a -- -- e b 0 8/c 5/a 7/c 14/c 0 8/c 5/a 7/c 14/c 1 14 8 10 3 2 9 0 a 4 6 5 7 5 7 2 d c

  27. Shortest Path Algorithms (Dijkstra’s Algorithm): step 3 a bcde 0 10/a 5/a -- -- e b 0 8/c 5/a 7/c 14/c 1 11 8 0 8/c 5/a 7/c 11/d 08/c5/a 7/c 11/d 10 3 2 9 0 a 4 6 5 7 5 7 2 d c

  28. Shortest Path Algorithms (Dijkstra’s Algorithm): step 4 a bcde 0 10/a 5/a -- -- e b 0 8/c 5/a 7/c 14/c 1 9 8 08/c5/a 7/c 11/d 08/c5/a 7/c9/d 10 3 2 9 0 a 4 6 5 7 5 7 2 d c

  29. Shortest Path Algorithms (Dijkstra’s Algorithm): Done a bcde 08/c5/a 7/c9/b e b 1 9 8 10 3 2 9 0 a 4 6 5 7 5 7 2 Minimum distance from a to b : 8 Minimum distance from a to c: 5 Minimum distance from a to d: 7 Minimum distance from a to e: 9 d c

  30. Example

  31. Dijkstra’s Algorithm Dijkstra(G: graph with vertices v0…vn-1and weights w[u][v]) // computes shortest distance of vertex 0 to every other vertex create a set vertexSet that contains only vertex 0 d[0] = 0 for (v = 1 through n-1) d[v] = infinity for (step = 2 through n) find the smallestd[v] such that v is not in vertexSet add v to vertexSet for (all vertices u not in vertexSet) if (d[u] >d[v] + w[v][u]) d[u] =d[v] + w[v][u]

  32. Shortest Path Algorithms Using a Priority Queue (Dijkstra’s Algorithm): step 1 e b 1 8 10 10 3 2 9 0 a 4 6 5 7 5 8 2 d c [5,c],[10,b]

  33. Shortest Path Algorithms (Dijkstra’s Algorithm): step 2 e b 1 14 8 10 3 2 9 0 a 4 6 5 7 5 7 2 d c [7,d],[8,b],[14,e]

  34. Shortest Path Algorithms (Dijkstra’s Algorithm): step 3 e b 1 11 8 10 3 2 9 0 a 4 6 5 7 5 7 2 d c [8,b],[11,e]

  35. Shortest Path Algorithms (Dijkstra’s Algorithm): step 4 c b 1 9 8 10 3 2 9 0 a 4 6 5 7 5 7 2 e d [9,c]

  36. Shortest Path Algorithms (Dijkstra’s Algorithm): Done c b 1 9 8 10 3 2 9 0 a 4 6 5 7 5 7 2 e d

  37. Dijkstra’s Algorithm • How to obtain the shortest paths? • At each vertex maintain a pointer that tells you the vertex from which you arrived.

  38. Outline • Introduction • Terminology • Implementing Graphs • Shortest Paths • Properties • Graph Traversals • Topological Sorting • Spanning Trees • Minimum Spanning Trees • Circuits

  39. Graph ADT • Create • Empty? • Number of vertices? • Number of edges? • Edge exists between two vertices? • Add a vertex • Add an edge • Delete a vertex (and any edges adjacent to it) • Delete an edge • Retrieve a vertex

  40. Classes for a Weighted UndirectedGraph • Vertex:??? • Edge: ??? • Graph: • organized collection of vertices and edges • Adjacency Matrix Implementation • Adjacency List Implementation

  41. A B C D E Adjacency Matrix Implementation • Edges • square matrix of edges Vertices Adjacency Matrix: indicates whether an edge exists between two vertices mapping of vertex labels to array indices Possible Java implementation: int [][] A;

  42. Adjacency Matrix Implementation • Directed graph: A[i][j] is 1 if there is an edge from vertex i to vertex j • Undirected graph: A[i][j] is 1 if there is an edge between vertex i and vertex j. What’s the relationship between A[i][j] and A[j][i]? • Weighted graph: A[i][j] gives the weight of the edge

  43. d e f g Example • Adjacency matrix?

  44. A B C D E Adjacency List Implementation Each vertex has a list of outgoing edges B D E A B C mapping of vertex labels to list of edges

  45. Adjacency List Implementation • For undirected graphs each edge appears twice. Why?

  46. Which Implementation? • Which implementation best supports common Graph Operations: • Is there an edge between vertex i and vertex j? • Find all vertices adjacent to vertex j • Which best uses space?

  47. Implementation: Edge Class Class Edge { private Integer v,w; // vertices private int weight; public Edge(Integer first, Integer second, intedgeWeight){ v = first; w = second; weight = edgeWeight; } public intgetWeight() { return weight; } public Integer getV() { return v; } public Integer getW() { return w; }

  48. Implementation: Graph Class class Graph { private intnumVertices; private intnumEdges; private Vector<TreeMap<Integer, Integer>> adjList; public Graph(intn) { numVertices = n; numEdges = 0; adjList = new Vector<TreeMap<Integer, Integer>>(); for (inti=0; i<numVertices; i++) { adjList.add(newTreeMap<Integer, Integer>()); } }

  49. Implementation: Graph Class public void addEdge(Integerv, Integer, w, int weight){ // precondition: the vertices v and w must exist //postcondition: the edge (v,w) is part of the graph adjList.get(v).put(w, weight); adjList.get(w).put(v, weight); numEdges++; }

More Related