Presentation is loading. Please wait.

Presentation is loading. Please wait.

GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1.

Similar presentations


Presentation on theme: "GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1."— Presentation transcript:

1 GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5,

2 Outline 1) What is a graph? 2) Terminology 3) Properties 4) Graph Types 5) Representations 6) Performance 7) BFS/DFS 8) Applications 2 Thursday, March 5, 2015

3 What is a graph? A graph defined by: a set of vertices (V) a set of edges (E) Vertices and edges can both store data Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL Example: A vertex represents an airport and stores the 3-letter airport code An edge represents a flight route between 2 airports and stores the mileage of the route

4 Terminology Thursday, March 5, End vertices (or endpoints) of an edge U and V are the endpoints of a Incident edges 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 (multiple) edges h and i are parallel edges Self-loop j is a self-loop XU V W Z Y a c b e d f g h i j

5 Terminology (2) 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 visited at most once Examples P 1 =(V–b->X–h->Z) is a simple path P 2 =(U–c->W–e->X–g->Y–f->W–d->V) is not a simple path, but is still a path Thursday, March 5, P1P1 XU V W Z Y a c b e d f g hP2P2

6 Graph Properties A graph G’ = (V’, E’) is a subgraph of G = (V, E) if V’ is contained in V and E’ is contained in E A graph is connected if there exists a path from each vertex to every other vertex in G. A cycle is a path that starts and ends at the same vertex A graph is acyclic if no subgraph is a cycle Thursday, March 5,

7 Graph Properties (2) A spanning tree is a subgraph that contains all the graph’s vertices in a single tree and enough edges to connect each vertex without cycles. Thursday, March 5, ORD PVD MIA DFW LGA ORD PVD MIA DFW LGA Graph G Spanning Tree for G

8 Graph Properties (3) A spanning forest is a subgraph that consists of a spanning tree in each connected component of a graph Spanning forests never contain cycles. Keep in mind that this might not be the “best” or shortest path to each node. Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL

9 Graph Properties (4) A graph G is a tree if and only if it satisfies any of the following 5 conditions: G has V-1 edges and no cycles G has V-1 edges and is connected G is connected, but removing any edge disconnects it G is acyclic, but adding any edges creates a cycle Exactly one simple path connects each pair of vertices in G Thursday, March 5,

10 Graph Proof 1 Prove that the sum of the degrees of all the vertices of some graph G equals twice the number of edges of G. Let V = {v 1, v 2, …, v p }, where p is the number of vertices in G. When computing the total sum of degrees D, we see that D = deg(v 1 ) + deg (v 2 ) + … + deg(v p ). Notice that each edge is counted twice in D: one for each of the two vertices incident on an edge. Thus D = 2*|E|, where |E| is the number of edges. Thursday, March 5,

11 Graph Proof 2 Using induction prove that in every connected graph: |E| ≥ |V| – 1 Base Case: A graph with one vertex will have 0 edges, and 0 ≥ 1 – 1 Assume: In a graph with k vertices, |E| ≥ k – 1 Let G be any connected graph with k + 1 vertices. We must show that |E| ≥ k. Let u be the vertex of minimum degree in G. If deg(u) = 1: u must be a leaf. Let G’ be G without u and its 1 incident edge. G’ is clearly still connected, because we only removed a leaf. By our assumption, G’ has at least k – 1 edges, which means that G has at least k edges. If deg(u) ≥ 2: Every vertex has at least two incident edges, so the total degree (D) of the graph is: D ≥ 2(k+1) Since we know from the last proof that D = 2*|E|: D ≥ 2(k+1)  2|E| ≥ 2(k+1)  |E| ≥ k + 1  |E| ≥ k True for 0, and for k+1 assuming k, so true for all n≥0. Thursday, March 5,

12 Graph Types Directed graph all the edges are directed ordered pair of vertices (u,v) first vertex u is the origin second vertex v is the destination e.g., a flight Undirected graph all the edges are undirected unordered pair of vertices (u,v) e.g., a flight route Thursday, March 5, ORDPVD flight AA 600 ORDPVD route 849 miles

13 Graph Types: Directed Graph Thursday, March 5, ORD DFW SFO LAX US 32 SW 223 AA 123 PA 23 LH 21

14 CS32 CS16 CS18 CS15 CS17 CS19 CS123 CS224 CS141 CS242 CS125 CS128 CS22 Acyclic = without cycles means ‘is a prerequisite for’ Thursday, March 5, We’ll talk much more about DAGs in future lectures… Directed Acyclic Graph (DAG)

15 Undirected Graph Thursday, March 5, ORD PVDF MIA DFW SFO LAX LGA HNL

16 Graph Representations Vertices usually stored in a list or hash set Three common methods of representing which vertices are adjacent: Edge list (or set) Adjacency lists (or sets) Adjacency matrix Thursday, March 5,

17 Edge List (or Set) Way of representing which vertices are adjacent to each other as a list of pairs Each element in the list is a single edge (a,b) from node a to node b Since order of this list doesn’t matter, we can use a single hash set instead to improve runtime Thursday, March 5, Edge List: [ (1,1), (1,2), (1,5), (2,3), (2,5), (3,4), (4,5), (4,6) ]

18 Adjacency Lists (or Sets) Another way of representing which vertices are adjacent to each other Each vertex has an associated list representing the vertices it neighbors Since order of these lists doesn’t matter, they could be hash sets instead to make lookup faster Thursday, March 5, [1,2,5][1,3,5][2,4][3,5,6][1,2,4][4]

19 Sets Thursday, March 5, Remember finding an element in a hashset is a constant operation: A = { (1,1), (1,2), (1,5), (2,3), (2,5), (3,4), (4,5), (4,6) } A.contains((4,5)) is O(1) Adjacency List elements can also be hashsets: B = [ {1, 2, 5}, {1, 3, 5}, {2, 4}, {3, 5, 6}, {1, 2, 4}, {4} ] B[2].contains(5) is still O(1) They can also be implemented as hashsets of hashsets! Oh boy!

20 Adjacency Matrices Another way of representing which vertices are adjacent to each other Matrix is n x n, where n is the number of nodes in the graph true = edge false = no edge If m[u][v] is true, then node u has an edge to node v (and if the graph is undirected, we can assume the opposite is true as well) Thursday, March 5,

21 Adjacency Matrices (2) Thursday, March 5, TTFFTF 2TFTFTF 3FTFTFF 4FFTFTT 5TTFTFF 6FFFTFF

22 Adjacency Matrices (3) We initialize the matrix to the predicted size of our graph (we can always expand the array later) When a vertex is added to the graph, we reserve a row and column of the matrix for that vertex When a vertex is removed, its row and column are set to false Since we can’t remove rows/columns from arrays, we keep a separate list of vertices to represent vertices actually present in the graph Thursday, March 5,

23 Main Methods of the Graph ADT Vertices and edges store values Ex: edge weights Accessor methods vertices() edges() incidentEdges(vertex) areAdjacent(v1, v2) endVertices(edge) opposite(vertex, edge) Update methods insertVertex(value) insertEdge(v1, v2) Note: Sometimes this function also takes a value! insertEdge(v1, v2, val) removeVertex(vertex) removeEdge(edge) Thursday, March 5,

24 Thursday, March 5, 2015 Big-O Performance: Edge SetAdjacency SetsAdjacency Matrix Overall Space|V| + |E| |V| 2 vertices() 1 1* edges()1*1*|E||V| 2 incidentEdges(v)|E|1*1*|V| areAdjacent (v1, v2)111 insertVertex(val)11|V| insertEdge(v1, v2)111 removeVertex(v)|E||V| removeEdge(v1, v2)111 Thursday, March 5, In all three approaches, we maintain an additional list or set of vertices. * in-place

25 Big-O Performance (Edge Set) Thursday, March 5, OperationRuntimeExplanation vertices()O(1) Return the set of vertices edges()O(1) Return the set of edges incidentEdges(v)O(|E|) Iterate through each edge and check if it contains vertex v areAdjacent(v1,v2)O(1) Check if edge (v1,v2) exists in the set insertVertex(v)O(1) Add vertex v to the vertex list insertEdge(v1,v2)O(1) Add element (v1,v2) to the set removeVertex(v)O(|E|) Iterate through each edge and remove it if it has vertex v removeEdge(v1,v2)O(1) Remove edge (v1,v2)

26 Big-O Performance (Adjacency Set) Thursday, March 5, OperationRuntimeExplanation vertices()O(1) Return the set of vertices edges()O(|E|) Concatenate each vertex with its subsequent vertices incidentEdges(v)O(1) Return v’s edge set areAdjacent(v1,v2)O(1) Check if v2 is in v1’s set insertVertex(v)O(1) Add vertex v to the vertex set insertEdge(v1,v2)O(1) Add v1 to v2’s edge set and vice versa removeVertex(v)O(|V|) Remove v from each of its adjacent vertices’ sets and remove v’s set removeEdge(v1,v2)O(1) Remove v1 from v2’s set and vice versa

27 Big-O Performance (Adjacency Matrix) Thursday, March 5, OperationRuntimeExplanation vertices()O(1) Return the set of vertices edges()O(|V| 2 ) Iterate through the entire matrix incidentEdges(v)O(|V|) Iterate through v’s row or column to check for true s Note: row/col are the same in an undirected graph. areAdjacent(v1,v2)O(1) Check index (v1,v2) for a true insertVertex(v)O(|V|)* Add vertex v to the matrix (*amortized) insertEdge(v1,v2)O(1) Set index (v1,v2) to true removeVertex(v)O(|V|) Set v’s row and column to false and remove v from the vertex list removeEdge(v1,v2)O(1) Remove v1 from v2’s set and vice versa

28 BFT/DFT Do you remember when we performed breadth first traversals and depth first traversals on trees? These traversals can also be applied to graphs! (A tree is just a special kind of graph, after all…) Can be used to traverse and “visit” nodes in a graph (BFT / DFT) Often used to search for a certain value in a graph (BFS / DFS) Thursday, March 5,

29 Thursday, March 5, 2015 Breadth First Traversal: Tree vs. Graph Thursday, March 5, 2015 function treeBFT(root): //Input: Root node of tree //Output: Nothing Q = new Queue() Q.enqueue(root) while Q is not empty: node = Q.dequeue() doSomething(node) enqueue node’s children function graphBFT(start): //Input: start vertex //Output: Nothing Q = new Queue() start.visited = true Q.enqueue(start) while Q is not empty: node = Q.dequeue() doSomething(node) for neighbor in node’s adjacent nodes: if not neighbor.visited: neighbor.visited = true Q.enqueue(neighbor) 29 doSomething(node) could print, add to list, decorate nodes, etc. Mark nodes as visited, otherwise you might loop forever! 29

30 Depth First Traversal One way to perform a DFT a graph is to use a stack instead of a queue as in BFT DFT can also be done recursively Thursday, March 5, function recursiveDFT(node): // Input: start node // Output: Nothing node.visited = true for neighbor in node’s adjacent vertices: if not neighbor.visited: recursiveDFT(neighbor)

31 Applications Flight networks GPS maps The internet pages are nodes links are edges Facebook Graph Modeling molecular structures atoms are nodes bonds are edges So many more!! Thursday, March 5,

32 Applications: Flight Paths Exist Problem: Given an undirected graph with airport (vertices) and flights (edges), determine whether it is possible to fly from one airport to another. Strategy: Use a breadth first search starting at the first node, and determine if the ending airport is ever visited. Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL PWM JFK

33 Applications: Flight Paths Exist (2) Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL PWM JFK Task: Is there a path from SFO to PVD?

34 Applications: Flight Paths Exist (2) Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL PWM JFK Task: Is there a path from SFO to PVD?

35 Applications: Flight Paths Exist (2) Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL PWM JFK Task: Is there a path from SFO to PVD?

36 Applications: Flight Paths Exist (2) Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL PWM JFK Task: Is there a path from SFO to PVD? YES, now how do we do it in code?

37 Flight Paths Exist Pseudocode Thursday, March 5, function pathExists(from, to): //Input: from: vertex, to: vertex //Output: true if path exists, false otherwise Q = new Queue() from.visited = true Q.enqueue(from) while Q is not empty: airport = Q.dequeue() if airport == to: return true for neighbor in airport’s adjacent nodes: if not neighbor.visited: neighbor.visited = true Q.enqueue(neighbor) return false

38 Applications: Flight Layovers Problem: Given an undirected graph with airport (vertices) and flights (edges), decorate the vertices with the least number of stops from a given source. If there is no way to get to a certain airport, decorate node with infinity. Strategy: Decorate each node with an initial ‘stop value’ of infinity. Use breadth first search to decorate each node with a ‘stop value’ of one greater than its previous. Thursday, March 5, ORD PVD MIA DFW SFO LAX LGA HNL PWM JFK

39 Flight Layover Pseudocode function numStops(G, source): //Input: G: graph, source: vertex //Output: Nothing //Purpose: decorate each vertex with the lowest number of // layovers from source. for every node in G: node.stops = infinity Q = new Queue() source.stops = 0 source.visited = true Q.enqueue(source) while Q is not empty: airport = Q.dequeue() for neighbor in airport’s adjacent nodes: if not neighbor.visited: neighbor.visited = true neighbor.stops = airport.stops + 1 Q.enqueue(neighbor) Thursday, March 5,


Download ppt "GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1."

Similar presentations


Ads by Google