Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.

Similar presentations


Presentation on theme: "CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1."— Presentation transcript:

1 CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1

2 Colorado State University Outline Introduction Terminology Shortest Paths Implementing Graphs Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 2

3 CS200 Algorithms and Data StructuresColorado State University Graphs What can this represent? A computer network Abstraction of a map Social network A collection of nodes and edges

4 CS200 Algorithms and Data StructuresColorado State University Directed Graphs Sometimes we want to represent directionality: Unidirectional network connections One way streets The web A collection of nodes and directed edges

5 CS200 Algorithms and Data StructuresColorado State University 5

6 CS200 Algorithms and Data StructuresColorado State University 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/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/brockmann2 006nature.pdf 6

7 CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 7

8 CS200 Algorithms and Data StructuresColorado State University Graph Terminology Vertices/ Nodes Edges 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 G=(V, E) v u e Vertices Edges Graph terminology: 14.1 in W&M, 9.1 in Rosen

9 CS200 Algorithms and Data StructuresColorado State University subgraph Graph Terminology A subgraph of 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

10 CS200 Algorithms and Data StructuresColorado State University Paths Path: a sequence of edges (e 1, e 2, e 3 ) is a path of length 3 from v 1 to v 4 In a simple graph a path can be represented as a sequence of vertices v1v1 v4v4 v2v2 v3v3 e1e1 e2e2 e3e3

11 CS200 Algorithms and Data StructuresColorado State University 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

12 CS200 Algorithms and Data StructuresColorado State University Directed Graphs Indegree: number of incoming edges Outdegree: number of outgoing edges w v

13 CS200 Algorithms and Data StructuresColorado State University 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. a b c d e f g G={{a,b,c,d,e,f,g},E} G 1 ={{a,b,c},E 1 } G 2 ={{d,e,f,g}, E 2 }

14 CS200 Algorithms and Data StructuresColorado State University 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

15 CS200 Algorithms and Data StructuresColorado State University Example 15 a f e g d c b deg(a) = 2 deg(b) = deg(f) = 4 deg(d) = 1 deg(e) = 3 deg(g) = 0

16 CS200 Algorithms and Data StructuresColorado State University Complete Graphs Simple graph that contains exactly one edge between each pair of distinct vertices. 16 Complete Graph

17 CS200 Algorithms and Data StructuresColorado State University Cycles The cycle C n, n ≥ 3, consists of n vertices v 1, v 2, …, v n and edges {v 1, v 2 }, {v 2, v 3 },…., and {v n-1, v n }. 17

18 CS200 Algorithms and Data StructuresColorado State University Wheels We obtain the wheel W n when we add an additional vertex to the cycle C n, for n ≤ 3, and connect this new vertex to each of the n vertices in C n, by new edges 18

19 CS200 Algorithms and Data StructuresColorado State University n -Cubes ( n -dimensional hypercube) Hypercube 001 100 000 010 011 110 111 101

20 CS200 Algorithms and Data StructuresColorado State University Bipartite Graphs A simple graph G is called bipartite, if its vertex set V can be partitioned into two disjoint sets V 1 and V 2 such that every edge in the graph connects a vertex in V 1 and a vertex in V 2. 20

21 CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 21

22 CS200 Algorithms and Data StructuresColorado State University 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

23 CS200 Algorithms and Data StructuresColorado State University 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

24 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm) a a b b e e c c d d 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6

25 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): Initialize 0 0 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 8 8 8 8 a b c e d a b c d e 0 ∞ ∞ ∞ ∞

26 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step 1 0 0 10 5 5 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 8 8 a b c e d a b c d e 0 10/a 5/a -- -- a b c d e 0 10/a 5/a -- -- a b c d e 0 ∞ ∞ ∞ ∞

27 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step 2 0 0 8 8 14 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b c e d a b c d e 0 10/a 5/a -- -- 0 8/c 5/a 7/c 14/c

28 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step 3 0 0 8 8 11 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b c e d a b c d e 0 10/a 5/a -- -- 0 8/c 5/a 7/c 14/c 0 8/c 5/a 7/c 11/d

29 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step 4 0 0 8 8 9 9 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b c e d a b c d e 0 10/a 5/a -- -- 0 8/c 5/a 7/c 14/c 0 8/c 5/a 7/c 11/d 0 8/c 5/a 7/c 9/d

30 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): Done 0 0 8 8 9 9 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b c e d a b c d e 0 8/c 5/a 7/c 9/b 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

31 CS200 Algorithms and Data StructuresColorado State University Example 31

32 CS200 Algorithms and Data StructuresColorado State University Dijkstra’s Algorithm Dijkstra(G: graph with vertices v 0 …v n-1 and 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 smallest d[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]

33 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms Using a Priority Queue (Dijkstra’s Algorithm): step 1 0 0 10 5 5 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 8 8 a b c e d [5,c],[10,b]

34 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step 2 0 0 8 8 14 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b c e d [7,d],[8,b],[14,e]

35 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step 3 0 0 8 8 11 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b c e d [8,b],[11,e]

36 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step 4 0 0 8 8 9 9 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b d c e [9,c]

37 CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): Done 0 0 8 8 9 9 5 5 7 7 10 3 3 2 2 1 1 9 9 7 7 2 2 5 5 4 4 6 6 a b d c e

38 CS200 Algorithms and Data StructuresColorado State University Dijkstra’s Algorithm How to obtain the shortest paths? –At each vertex maintain a pointer that tells you the vertex from which you arrived.

39 CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 39

40 CS200 Algorithms and Data StructuresColorado State University 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

41 CS200 Algorithms and Data StructuresColorado State University Classes for a Weighted Undirected Graph Vertex:??? Edge: ??? Graph: –organized collection of vertices and edges 1.Adjacency Matrix Implementation 2.Adjacency List Implementation

42 CS200 Algorithms and Data StructuresColorado State University Adjacency Matrix Implementation A BC E D mapping of vertex labels to array indices LabelIndex A0 B1 C2 D3 E4 01234 001010 100001 210000 301000 400100 Adjacency Matrix: indicates whether an edge exists between two vertices Vertices Edges square matrix of edges Possible Java implementation: int [][] A;

43 CS200 Algorithms and Data StructuresColorado State University 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

44 CS200 Algorithms and Data StructuresColorado State University Example Adjacency matrix? d e f g defg d0111 e1001 f1000 g1100

45 CS200 Algorithms and Data StructuresColorado State University Adjacency List Implementation A BC E D mapping of vertex labels to list of edges IndexLabel 0A 1B 2C 3D 4E B D E A B C Each vertex has a list of outgoing edges

46 CS200 Algorithms and Data StructuresColorado State University Adjacency List Implementation For undirected graphs each edge appears twice. Why?

47 CS200 Algorithms and Data StructuresColorado State University 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?

48 CS200 Algorithms and Data StructuresColorado State University Implementation: Edge Class Class Edge { private Integer v,w; // vertices private int weight; public Edge(Integer first, Integer second, int edgeWeight){ v = first; w = second; weight = edgeWeight; } public int getWeight() { return weight; } public Integer getV() { return v; } public Integer getW() { return w; }

49 CS200 Algorithms and Data StructuresColorado State University Implementation: Graph Class class Graph { private int numVertices; private int numEdges; private Vector > adjList;TreeMap public Graph(int n) { numVertices = n; numEdges = 0; adjList = new Vector >(); for (int i=0; i<numVertices; i++) { adjList.add(new TreeMap ()); } }

50 CS200 Algorithms and Data StructuresColorado State University Implementation: Graph Class public void addEdge(Integer v, 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++; }

51 CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 51

52 CS200 Algorithms and Data StructuresColorado State University Graph Traversal ABCD EFGH IJKL MNOP

53 CS200 Algorithms and Data StructuresColorado State University Depth First Search ABCD EFGH IJKL MNOP

54 CS200 Algorithms and Data StructuresColorado State University Graph Traversal Depth First Search (DFS) dfs(in v:Vertex) mark v as visited for (each unvisited vertex u adjacent to v) dfs(u) Need to track visited nodes Order of visiting nodes is not completely specified Is there a difference between directed undirected graphs? Which graph implementation? Depth first search

55 CS200 Algorithms and Data StructuresColorado State University Iterative DFS dfs(in v:Vertex) s – stack for keeping track of active vertices s.push(v) mark v as visited while(!s.isEmpty()) { if (no unvisited vertices adjacent to the vertex on top of the stack) { s.pop() \\backtrack\\backtrack else { select unvisited vertex u adjacent to vertex on top of the stack s.push(u) mark u as visited } }

56 CS200 Algorithms and Data StructuresColorado State University 5 4 0 1 2 3 Graph Traversal – Breadth First Search (BFS) ABCD EFGH IJKL MNOP Breath First Search

57 CS200 Algorithms and Data StructuresColorado State University BFS Similar to level order tree traversal DFS is a last visited first explored strategy BFS is a first visited first explored strategy

58 CS200 Algorithms and Data StructuresColorado State University BFS bfs(in v:Vertex) q – queue of nodes to be processed q.enque(v) mark v as visited while(!q.isEmpty()) { w = q.dequeue() for (each unvisited vertex u adjacent to w) { mark u as visited q.enqueue(u) } }

59 CS200 Algorithms and Data StructuresColorado State University Trace this example 59 5 4 0 1 2 3 ABCD EFGH IJKL MNOP bfs(in v:Vertex) q – queue of nodes to be processed q.enque(v) mark v as visited while(!q.isEmpty()) { w = q.dequeue() for (each unvisited vertex u adjacen t to w) { mark u as visited q.enqueue(u) } }

60 CS200 Algorithms and Data StructuresColorado State University Graph Traversal Properties of BFS and DFS: –Visit all vertices that are reachable from a given vertex –Therefore DFS(v) and BFS(v) visit a connected component Computation time for DFS, BFS for a connected graph: O(|V| + |E|)

61 CS200 Algorithms and Data StructuresColorado State University Reachability –v is reachable from u if there is a (directed) path from u to v –solve using BFS or DFS Transitive Closure (G*) –G* has edge from u to v if v is reachable from u.


Download ppt "CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1."

Similar presentations


Ads by Google