620 likes | 839 Views
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
E N D
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 • Abstraction of a map • Social network A collection of nodes and edges
Directed Graphs A collection of nodes and directed edges Sometimes we want to represent directionality: • Unidirectional network connections • One way streets • The web
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
Outline • Introduction • Terminology • Implementing Graphs • Shortest Paths • Properties • Graph Traversals • Topological Sorting • Spanning Trees • Minimum Spanning Trees • Circuits
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
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
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
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
Directed Graphs Indegree: number of incoming edges Outdegree: number of outgoing edges w v
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}
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.
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
Complete Graphs • Simple graph that contains exactly one edge between each pair of distinct vertices. Complete Graph
Cycles The cycle Cn, n≥ 3, consists of n vertices v1, v2, …, vnand edges {v1, v2}, {v2, v3},…., and {vn-1, vn}.
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
n-Cubes (n-dimensional hypercube) Hypercube 110 111 101 100 001 010 011 000
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.
Outline • Introduction • Terminology • Implementing Graphs • Shortest Paths • Properties • Graph Traversals • Topological Sorting • Spanning Trees • Minimum Spanning Trees • Circuits
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
Shortest Path Algorithms (Dijkstra’s Algorithm) • Algorithm • Maintain array d (minimum distance estimates) • Init: d[s]=0, d[v]=vV-s • Priority queue of vertices not yet visited • select minimum distance vertex, visit v, update neighbors
Shortest Path Algorithms (Dijkstra’s Algorithm) 1 e b 10 3 2 9 a 4 6 5 7 c d 2
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
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
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
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
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
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
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]
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]
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]
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]
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]
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
Dijkstra’s Algorithm • How to obtain the shortest paths? • At each vertex maintain a pointer that tells you the vertex from which you arrived.
Outline • Introduction • Terminology • Implementing Graphs • Shortest Paths • Properties • Graph Traversals • Topological Sorting • Spanning Trees • Minimum Spanning Trees • Circuits
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
Classes for a Weighted UndirectedGraph • Vertex:??? • Edge: ??? • Graph: • organized collection of vertices and edges • Adjacency Matrix Implementation • Adjacency List Implementation
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;
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
d e f g Example • Adjacency matrix?
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
Adjacency List Implementation • For undirected graphs each edge appears twice. Why?
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?
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; }
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>()); } }
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++; }