Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Graphs: Networks CIS 265/506 - Chapter 14 Graphs.

Similar presentations


Presentation on theme: "Data Structures Graphs: Networks CIS 265/506 - Chapter 14 Graphs."— Presentation transcript:

1 Data Structures Graphs: Networks CIS 265/506 - Chapter 14 Graphs

2 Networks A Network is a graph whose edges are weighted
Also known as a weighted graph Meaning of “Weight” is application dependent (could be distance, time, money, flow,…) CIS 265/506 - Chapter 14 Graphs

3 Networks 548 D B 623 320 360 A 200 F 245 345 555 C E 467 Example of a weighted graph. Assume distance between nodes A & B is 623 miles CIS 265/506 - Chapter 14 Graphs

4 Networks A B C D E F A B C D E F A 0 623 354 0 0 0 623 0 200 548 0 0 B
B C D E F Vertex Vector Adjacency Matrix Representing a weighted graph with an Adjacency Matrix (observe symmetric redundancy) CIS 265/506 - Chapter 14 Graphs

5 Networks Vertex List Adjacency List A B C D E F B 623 C 345 A 623
Representing a weighted graph with an Adjacency List. Vertex List Adjacency List A B 623 C 345 B A 623 C 200 D 548 C A 345 B 200 D 360 E 467 D B 548 C 360 E 245 F 320 E C 467 D 254 F 555 F D 320 E 555

6 Minimum Spanning Tree Consider a graph G=(V,E)
A spanning tree T for G is a tree that contains all the vertices of V and some edges from E. A Minimum Spanning Tree T (MST) of a network G is such that the sum of the weights is guaranteed to be minimal If the weights in the network are unique, then there is only one minimal spanning tree CIS 265/506 - Chapter 14 Graphs

7 Minimum Spanning Tree Examples
Assume we have a computer network and the edges are labeled with the distance between nodes. A minimal spanning tree of the network would give us the shortest length of cable required to connect all the computers CIS 265/506 - Chapter 14 Graphs

8 Minimum Spanning Tree - Example
5 A B C D E F 3 6 4 2 Find a spanning Tree for the following graph. CIS 265/506 - Chapter 14 Graphs

9 Minimum Spanning Tree - Example
1. Start with any vertex. If the vertices are key-sequenced, start with the lowest 5 A B C D E F 3 6 4 2 A CIS 265/506 - Chapter 14 Graphs

10 Minimum Spanning Tree - Example
2. Add in the adjacent vertex with the minimum weighted edge, in this case: AC A B C D E F 3 6 4 2 5 A 3 C CIS 265/506 - Chapter 14 Graphs

11 Minimum Spanning Tree - Example
3. From these vertices, locate the next edge with a minimum weight. AB is weighted 6, BC is weighted 2, CD is 3, CE is 4. The minimum is BC 5 A B C D E F 3 6 4 2 B A 2 3 C CIS 265/506 - Chapter 14 Graphs

12 Minimum Spanning Tree - Example
4. Continue on until all vertices are included. Note we do not consider edges back to vertices that have been visited already (i.e. we would not consider edge AB) 5 A B C D E F 3 6 4 2 B D A 3 2 3 C CIS 265/506 - Chapter 14 Graphs

13 Minimum Spanning Tree - Example
5 A B C D E F 3 6 4 2 B D A 3 2 2 3 C E CIS 265/506 - Chapter 14 Graphs

14 Minimum Spanning Tree - Example
5 A B C D E F 3 6 4 2 Minimum Spanning Tree Solution 5 B D 3 3 A 3 2 2 F 3 5 C E 4 CIS 265/506 - Chapter 14 Graphs

15 Minimum Spanning Tree begin T ← ϕ (spanning tree)
Kruskal’s Algorithm: Finding a Minimum Spanning Tree for weighted graph G=(V, E) begin T ← ϕ (spanning tree) VS ← ϕ (vertices in the solution) Construct a min-Heap containing all the edges; for each vertex v ϵ V do add {v } to VS; while |VS|>1 do begin choose (v, w), an edge in min-Heap of lowest cost; delete (v, w) from min-Heap; if v and w are in different sets W1 and W2 in VS then begin replace W1 and W2 in VS by W1  W2 add (v, w) to T; end; The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

16 Minimum Spanning Tree v1 v2 v7 v3 v6 v5 v4 20 15 4 23 1 9 36 25 16 28
17 Find a Minimum Spanning Tree for graph above. The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

17 Minimum Spanning Tree min-Heap Edge Cost (v1, v7) 1 (v4, v5) 17
3 (v1, v2) 20 (v2, v7) 4 (v1, v6) 23 (v3, v7) 9 (v5, v7) 25 (v2, v3) 15 (v5, v6) 28 (v4, v7) 16 (v6, v7) 36 Representing ordered sequence of weighted edges (use a min-Heap instead) The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

18 Minimum Spanning Tree Edge Action Sets in VS (connected components)
(v1, v7) Add { v1,v7 }, { v2 }, { v3 }, { v4 }, { v5 }, { v6 } (v3, v4) { v1,v7 }, { v2 }, { v3, v4 }, { v5 }, { v6 } (v2, v7) { v1,v7, v2 }, { v3, v4 }, { v5 }, { v6 } (v3, v7) { v1,v7, v2, v3, v4 }, { v5 }, { v6 } (v2, v3) Reject (v4, v7) (v4, v5) { v1,v7, v2, v3, v4, v5 }, { v6 } (v1, v2) (v1, v6) { v1,v7, v2, v3, v4, v5, v6 } We begin with a forest of individual nodes and finish with a single unified component including all the nodes. An edge is added to combine two disjoint components. An edge is rejected when it forms a closed circuit. The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

19 Minimum Spanning Tree v1 v2 v7 v3 v6 v5 v4 4 23 1 9 3 17
A Minimum Spanning Tree for example graph. Total weight is 57. CIS 265/506 - Chapter 14 Graphs

20 Shortest Path Algorithm
There are occasions when we wish to find the shortest path between various vertices. Edsger Dijkstra developed a classic algorithm to solve the single source path routing problem For a node v in the graph G, the algorithm finds the path with lowest cost between that vertex v and every other vertex in G. This is known as “Dijkstra’s Shortest Path Algorithm” Dijkstra, E.W. “A note on two problems in connexion with graphs,” Numerische Mathematik, Vol. 1(1959), CIS 265/506 - Chapter 14 Graphs

21 Shortest Path Algorithm - Example
We want to find the shortest path between A and any other vertex. The total path’s cost to each vertex is denoted with “T: n” 5 A B C D E F 3 6 4 2 T: 6 B 6 T: 0 A A Add node C whose weight is T:3 (better than B’s T:6 ) 3 3 C C T: 3 T: 3 Possible Paths Finding Shortest Path

22 Shortest Path Algorithm - Example
Visited Nodes = { A, C } Adjacent to Visited are { B, D, E } Choose node B with weight T: 5 5 A B C D E F 3 6 4 2 T: 6 T: 6 D B 6 B T: 5 T: 0 T: 5 3 A 2 2 A 3 T: 7 C 3 E 4 T: 3 C T: 3 Possible Paths Shortest Paths

23 Shortest Path Algorithm - Example
5 A B C D E F 3 6 4 2 Visited Nodes = { A, B, C } Adjacent to Visited are { D, E } Choose node D with weight T: 6 T: 6 T: 6 5 D B T: 6 B D T: 5 T: 0 T: 5 3 A 2 2 3 A 3 T: 7 C 3 E 4 T: 3 C T: 3 Possible Paths Shortest Paths

24 Shortest Path Algorithm - Example
Visited Nodes = { A, B, C, D } Adjacent to Visited are { E, F } Choose node E with weight T: 7 5 A B C D E F 3 6 4 2 T: 6 T: 6 D B 3 T: 6 B D T: 5 T: 9 T: 0 T: 5 3 F A 2 2 2 3 A T: 8 3 C 3 E 4 T: 7 T: 3 T: 7 C E T: 3 4 Possible Paths Shortest Paths

25 Shortest Path Algorithm - Example
5 A B C D E F 3 6 4 2 Visited Nodes = { A, B, C, D, E } Adjacent to Visited is { F } Choose node F with weight T: 9 T: 6 T: 6 D B 3 T: 6 T: 9 B D T: 5 3 T: 0 T: 5 3 F A 2 T: 9 2 3 F T: 12 A 3 5 T: 7 C 3 E 4 T: 7 T: 3 C E T: 3 4 Possible Paths Shortest Paths

26 Shortest Path Algorithm - Example
Informally, we have done the following: Insert the first vertex into the solution tree From every vertex already in the solution tree, examine the total path length to all adjacent vertices not in the tree Select the edge with the minimum total path weight and insert it in to the tree Repeat step two until all vertices are in the tree CIS 265/506 - Chapter 14 Graphs

27 Shortest Path Algorithm
Dijkstra’s Algorithm Input: A directed graph G=(V, E), a source node v0 in V, and a function l from edges to nonnegative reals. We take l(vi, vj) to be + if l(vi,vj) is not an edge, and l(v,v)=0. Output: For each v in V, the minimum over all paths P from v0 to v of the sum of the labels of the edges of P. Method: We construct a set S  V such that the shortest path from the source to each vertex v in S lies completely in S. The Array D[v] contains the cost of the current shortest path from v0 to v passing through the vertices of S. Details follow The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

28 Shortest Path Algorithm
Dijkstra’s Algorithm begin S ← { v0 } (source node) D[ v0 ] ← 0 for each vertex v ϵ ( V – v0 ) do D[ v ] ← l(v0 v); while ( S  V ) do begin choose a vertex w in ( V – S ) such that D[ w ] is a minimum; add w to S; for each v in (V – S) do D[ v ] ← MIN( D[ v ], D[ w ] + l( w, v ) ) [w is in S] end; The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

29 Shortest Path Algorithm
2 Dijkstra’s Algorithm V0 V1 3 10 7 V2 6 V4 4 V3 5 Iteration S w D[w] D[v1] D[v2] D[v3] D[v4] Initial { v0 } - 2 + 10 1 { v0, v1 } v1 5 9 { v0, v1, v2 } v2 3 { v0, v1, v2, v3 } v3 4 { v0, v1, v2, v3, v4 } v4 The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

30 Shortest Path Algorithm
2 Dijkstra’s Algorithm V0 V1 3 7 V2 V4 4 V3 S D[v0] D[v1] D[v2] D[v3] D[v4] Solution { v0, v1, v2, v3, v4 } 2 5 9 The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

31 Topological Sort Topological sorting is a partial-ordering strategy useful for defining the sequence in which events should /could happen. Examples: Course prerequisites must be completed prior to certain course. Morning dressing up schedule Functional Dependencies A DAG (directed acyclic graph) can be used to represent the problem (and its solution) Reference: CIS 265/506 - Chapter 14 Graphs

32 Topological Sort Possible valid orders: Solution 1 Solution 2
CIS151, CIS260, CIS265, CIS335, CIS340, CIS368, CIS345, CIS485 CIS340, CIS345, CIS335, CIS368,

33 Topological Sort

34 Topological Sort L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S insert n into L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then output error message (graph has at least one cycle) else output message (proposed topologically sorted order: L) Reference: CIS 265/506 - Chapter 14 Graphs

35 Topological Sort The graph shown to the left has many valid topological sorts, including: 7, 5, 3, 11, 8, 2, 9, 10 (visual left-to-right, top-to-bottom) 3, 5, 7, 8, 11, 2, 9, 10 (smallest-numbered vertex first) 3, 7, 8, 5, 11, 10, 2, 9 5, 7, 3, 8, 11, 10, 9, 2 (fewest edges first) 7, 5, 11, 3, 10, 8, 9, 2 (largest-numbered vertex first) 7, 5, 11, 2, 3, 8, 9, 10 Reference: CIS 265/506 - Chapter 14 Graphs

36 Biconnected Components
Definition A graph G=(E, V) is biconnected if for every triple of vertices v1, v2, va there exists a path between v1 and v2 not containing va. V2 V5 V2 V1 V3 V4 V1 V4 V6 V3 Biconnected Not Biconnected The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

37 Biconnected Components
Definition A vertex a is said to be an articulation point of G if there exist vertices v and w such that every path between v and w contains a. An undirected connected graph G is biconnected iff it has no articulation points. Biconnected Components: { v1, v2, v3 } { v4, v5, v6 } { v2, v4 } V4 and V2 are articulation points V5 V2 V4 V1 V6 V3 Not Biconnected The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

38 Finding Articulation Points
Procedure dfsLabeling ( Vertice v ) { mark v “visited”; DFSNUMBER[ v ] ⟵ COUNT; COUNT ⟵ COUNT + 1; LOW[ v ] ⟵ DFSNUMBER[ v ]; for each vertex w on adjacencyList(v) do { if w is marked “not visited” then { add edge ( v, w ) to T; FATHER[ w ] ⟵ v dfsLabeling ( w ); if LOW[ w ]  DFSNUMBER[ v ] then { a biconnected component has been found; v is a n articulation point; } else { if w is not FATHER[ v ] then LOW[w] ⟵ MIN ( LOW[ v ], FDSNUMBER[ w ] ); The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

39 Finding Articulation Points
(a) Graph G (b) A DFS Labeled Tree for graph G The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

40 Finding Articulation Points
(b) Biconnected Components (a) Graph G The Design and Analysis of Computer Algorithms. Aho, Hopcroft, Ullman

41 Appendix. Finding Articulation Points – Biconnected Components
public class Driver { /** * Author: Matos * Date: 8/9/2011 * Goal: (1) Implementing a graph using adjacency list (2) traversing graph using DFS_Numbering to * detect articulation points and biconnected components. (see ppt notes BG & VM 265) */ public static void main(String[] args) { Graph<String> graph = new Graph<String>(); graph.addVertex("V0"); graph.addVertex("V1"); graph.addVertex("V2"); graph.addVertex("V3"); graph.addVertex("V4"); graph.addVertex("V5"); graph.addVertex("V6"); graph.addVertex("V7"); graph.addVertex("V8"); graph.addVertex("V9"); graph.addEdge(1,2); graph.addEdge(2,9); graph.addEdge(1,9); graph.addEdge(2,3); graph.addEdge(3,8); graph.addEdge(2,8); graph.addEdge(3,4); graph.addEdge(4,5); graph.addEdge(5,6); graph.addEdge(5,7); graph.addEdge(6,4); graph.addEdge(7,4); graph.showData(); // traversing graph using SIMPLE DFS graph.clearVisitedMarkers(); System.out.println("\nDFS"); graph.dfs(1); // traversing graph using DFS_NUMBERING to find // all biconnected components System.out.println("\nDFS Labeling"); graph.dfsLabeling(1); graph.showDataLabels(); }//main }//Driver

42 Appendix. Finding Articulation Points – Biconnected Components
import java.util.Comparator; public class Edge implements Comparable { int v1; int v2; public Edge(int newV1, int newV2){ if (newV1 <= newV2){ v1= newV1; v2= newV2; } else { v1= newV2; v2= newV1; public void showData(){ System.out.printf("\n[v%d, v%d]", v1, v2); @Override public int compareTo(Object otherEdge) { Edge e = (Edge)otherEdge; if ((v1==e.v1 && v2==e.v2) || (v1==e.v2 && v2==e.v1) ) return 0; else return 1;

43 Appendix. Finding Articulation Points – Biconnected Components
package csu.matos; import java.util.ArrayList; import java.util.Stack; public class Graph<E> { private ArrayList<E> vertices; private ArrayList< ArrayList<Integer> > neighbors; private ArrayList<Boolean> visitedMarkers; private ArrayList<Integer> father; private ArrayList<Integer> dfsNumber; private ArrayList<Integer> low; int count = 1; Stack<Edge> stack = new Stack<Edge>(); Stack<Edge> stackAlreadyVisited = new Stack<Edge>(); public Graph() { vertices = new ArrayList<E>(); neighbors = new ArrayList< ArrayList<Integer> > (); visitedMarkers = new ArrayList<Boolean>(); father = new ArrayList<Integer>(); dfsNumber = new ArrayList<Integer>(); low = new ArrayList<Integer>(); } public void addVertex(E newVertice){ vertices.add(newVertice); neighbors.add( new ArrayList<Integer>() ); visitedMarkers.add(false); public void addEdge(int v1, int v2){ ArrayList<Integer> adjacent1 = neighbors.get(v1); if (!adjacent1.contains(v2)) neighbors.get(v1).add(v2); ArrayList<Integer> adjacent2 = neighbors.get(v2); if (!adjacent2.contains(v1)) neighbors.get(v2).add(v1); public void showData() { for(int i=0; i<vertices.size(); i++){ System.out.printf("\nVertice[%d]= %s" , i, vertices.get(i));

44 Appendix. Finding Articulation Points – Biconnected Components
for(int i=0; i<vertices.size(); i++){ System.out.printf("\nneighbors[%d] >> " , i); ArrayList<Integer> adjacent = neighbors.get(i); for(int j=0; j<adjacent.size(); j++){ System.out.printf(" %d", adjacent.get(j)); } public void clearVisitedMarkers(){ for (int i=0; i<visitedMarkers.size(); i++){ visitedMarkers.set(i, false); father.add(0); low.add(0); dfsNumber.add(0); public void dfs(int v){ visitedMarkers.set(v, true); int n; ArrayList<Integer> adjacent = neighbors.get(v); for (int i=0; i<adjacent.size(); i++){ n = adjacent.get(i); if (!visitedMarkers.get(n)){ System.out.printf(" [v%d v%d] ", v, n); dfs(n); // /////////////////////////////////////////////////////////////// public void dfsLabeling(int v){ dfsNumber.set(v, count); count++; low.set(v, dfsNumber.get(v)); int w; w = adjacent.get(i); //Stacking edges pushEdgeBiconnectedComponentStack(v, w);

45 Appendix. Finding Articulation Points – Biconnected Components
if (!visitedMarkers.get(w)){ // adding node n to solution tree n System.out.printf(" [v%d v%d] ", v, w); father.set(w, v); dfsLabeling(w); System.out.printf("\nback (v,w) (%d,%d)", v, w); System.out.printf("\nlow[%d]: %d low[%d]:%d", v, low.get(v), w, low.get(w) ); if ( low.get(w) >= dfsNumber.get(v)) { System.out.printf("\nBiconnected comp. found.%d Articulation: %d",w, v); popEdgeBiconnectedComponentStack(v,w); } int minValueVW = Math.min(low.get(v), low.get(w)); low.set(v, minValueVW); System.out.printf("\nset1 low[%d]: %d", v, low.get(v)); } else { if ( w != father.get(v)){ int minValueVW = Math.min(low.get(v), dfsNumber.get(w)); System.out.printf("\n!father>> low[%d]: %d dfsN[%d]:%d", v, low.get(v), w, dfsNumber.get(w) ); System.out.printf("\nset2 low[%d]: %d", v, low.get(v)); }//dfsLabeling private void popEdgeBiconnectedComponentStack(int v, int w) { Edge e1; if (v < w) e1 = new Edge(v, w); else e1 = new Edge(w, v); Edge e = null; while (!stack.isEmpty()){ e = stack.pop(); // copy visited/popped edges to stackAlreadyVisisted stackAlreadyVisited.push(e); System.out.printf(" \n>>Binconn [%d, %d] ", e.v1, e.v2); if (e.compareTo(e1)==0){ break;

46 Appendix. Finding Articulation Points – Biconnected Components
private void pushEdgeBiconnectedComponentStack(int v, int w) { Edge e1; if (v < w) e1 = new Edge(v,w); else e1 = new Edge(w,v); // is this edge already in the stack? boolean found = false; int i=0; Edge e; while ((i<stack.size()) && !found){ e = stack.get(i); if (e.compareTo(e1)==0) found = true; i++; } // has this edge been used already by other component? boolean found2 = false; int i2=0; Edge e2; while ((i2<stackAlreadyVisited.size()) && !found2){ e2 = stackAlreadyVisited.get(i2); if (e2.compareTo(e1)==0) found2 = true; i2++; // insert only new edges (only ONCE!!!) if (!found && !found2) stack.push(e1); public void showDataLabels(){ for (int i=0; i<vertices.size(); i++){ System.out.printf("\nv%d low:%d dfsN:%d father:%d" , i, low.get(i), dfsNumber.get(i), father.get(i) );

47 Appendix. Finding Articulation Points – Biconnected Components
(console – dfsLabeling ) Biconnected comp. found.5 Articulation: 4 >>Binconn [4, 7] Vertice[0]= V0 >>Binconn [5, 7] Vertice[1]= V1 >>Binconn [4, 6] Vertice[2]= V2 >>Binconn [5, 6] Vertice[3]= V3 >>Binconn [4, 5] Vertice[4]= V4 set1 low[4]: 6 Vertice[5]= V5 !father>> low[4]: 6 dfsN[6]:8 Vertice[6]= V6 set2 low[4]: 6 Vertice[7]= V7 !father>> low[4]: 6 dfsN[7]:9 Vertice[8]= V8 Vertice[9]= V9 back (v,w) (3,4) neighbors[0] >> low[3]: 2 low[4]:6 neighbors[1] >> Biconnected comp. found.4 Articulation: 3 neighbors[2] >> >>Binconn [3, 4] neighbors[3] >> set1 low[3]: 2 neighbors[4] >> back (v,w) (2,3) neighbors[5] >> low[2]: 1 low[3]:2 neighbors[6] >> Biconnected comp. found.3 Articulation: 2 neighbors[7] >> >>Binconn [2, 8] neighbors[8] >> >>Binconn [3, 8] neighbors[9] >> >>Binconn [2, 3] DFS Labeling set1 low[2]: 1 [v1 v2] [v2 v9] !father>> low[2]: 1 dfsN[8]:5 !father>> low[9]: 3 dfsN[1]:1 set2 low[2]: 1 set2 low[9]: 1 back (v,w) (1,2) back (v,w) (2,9) low[1]: 1 low[2]:1 low[2]: 2 low[9]:1 Biconnected comp. found.2 Articulation: 1 set1 low[2]: 1 [v2 v3] [v3 v8] >>Binconn [1, 9] !father>> low[8]: 5 dfsN[2]:2 >>Binconn [2, 9] set2 low[8]: 2 >>Binconn [1, 2] back (v,w) (3,8) set1 low[1]: 1 low[3]: 4 low[8]:2 !father>> low[1]: 1 dfsN[9]:3 set1 low[3]: 2 [v3 v4] [v4 v5] [v5 v6] set2 low[1]: 1 !father>> low[6]: 8 dfsN[4]:6 v0 low:0 dfsN:0 father:0 set2 low[6]: 6 v1 low:1 dfsN:1 father:0 back (v,w) (5,6) v2 low:1 dfsN:2 father:1 low[5]: 7 low[6]:6 v3 low:2 dfsN:4 father:2 set1 low[5]: 6 [v5 v7] v4 low:6 dfsN:6 father:3 !father>> low[7]: 9 dfsN[4]:6 v5 low:6 dfsN:7 father:4 set2 low[7]: 6 v6 low:6 dfsN:8 father:5 back (v,w) (5,7) v7 low:6 dfsN:9 father:5 low[5]: 6 low[7]:6 v8 low:2 dfsN:5 father:3 set1 low[5]: 6 v9 low:1 dfsN:3 father:2 back (v,w) (4,5) low[4]: 6 low[5]:6


Download ppt "Data Structures Graphs: Networks CIS 265/506 - Chapter 14 Graphs."

Similar presentations


Ads by Google