Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs.

Similar presentations


Presentation on theme: "Graphs."— Presentation transcript:

1 Graphs

2 What is a Graph? A graph G = (V,E) is composed of: V: set of vertices
E: set of edges connecting the vertices in V An edge e = (u1,u2) is a pair of vertices Examples: Bus routes Transitions in a finite state machine Course prerequisites

3 What is a Graph? If the elements of E(G) are ordered pairs
then G is called a directed graph or digraph; else G is called an undirected graph In an undirected graph, the pairs (u1,u2) and (u2,u1) represent the same edge

4 What is a Graph? 2 3 1 4 5 2 3 1 4 5 Undirected Graph Directed Graph

5 Applications Electronic circuits Transportation networks
Printed circuit board Integrated circuit Transportation networks Highway network Flight network Computer networks Local area network Internet Web JFK LAX LAX STL HNL DFW

6 Terminology Adjacent and Incident
If (v0, v1) is an edge in an undirected graph, v0 and v1 are adjacent The edge (v0, v1) is incident on vertices v0 and v1 If (v0, v1) is an edge in a directed graph v0 is adjacent to v1, and v1 is adjacent from v0 The edge (v0, v1) is incident on v0 and v1 v0 v1 v0 v1

7 Terminology (cont.) Degree of a Vertex
The degree of a vertex is the number of edges incident to that vertex For directed graph, - the in-degree of a vertex v is the number of edges that have v as the head - the out-degree of a vertex v is the number of edges that have v as the tail if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is Why? Since adjacent vertices each count the adjoining edge, it will be counted twice

8 Examples 1 2 1 3 2 G1 G2 Undirected Graph Directed Graph 3
in:1, out: 1 3 1 2 1 in: 1, out: 2 3 3 3 2 in: 1, out: 0 G1 G2

9 Terminology (cont.) Path Simple path Examples P1 X U V W Z Y a c b e d
sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. Simple path no repeated vertices Examples P1=(V, X, Z) is a simple path P2=(U, W, X, Y, W, V) is a path that is not simple P1 X U V W Z Y a c b e d f g h P2

10 Terminology (cont.) Cycle Simple cycle Examples C1 X U V W Z Y a c b e
circular sequence of vertices, where that the last vertex is the same as the first vertex . Simple cycle simple path, except that the last vertex is the same as the first vertex Examples C1=(V, X, Y, W, U, V) is a simple cycle C2=(U, W, X, Y, W, V, U) is a cycle that is not simple C1 X U V W Z Y a c b e d f g h C2

11 Terminology (cont.) Subgraph Spanning subgraph
A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G Spanning subgraph A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph S Spanning subgraph

12 Subgraphs Examples 1 2 3 G1 1 2 G2 (a) Some of the subgraph of G1
1 2 3 1 2 3 3 1 2 1 2 (i) (ii) (iii) (iv) (a) Some of the subgraph of G1 G1 1 2 1 1 2 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G2 G2

13 Non connected graph with two connected components
Terminology (cont.) Connected graph Non connected graph with two connected components Connected graph there is a path between every pair of vertices Connected component it is the maximal connected subgraph of G

14 Terminology (cont.) Tree Forest it is a connected graph without cycles
// This definition is different from the rooted tree (in chapter 9) Forest collection of trees (i.e. it is an undirected graph without cycles) tree forest

15 Terminology (cont.) A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree

16 Terminology (cont.) A complete graph is a graph in which all pairs of vertices are adjacent How many total edges in a complete graph? Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice! Therefore, intuitively, m = n(n -1)/2 edges. Therefore, if a graph is not complete, m < n(n -1)/2 n = 5 m = (5*4)/2

17 Graph Representation Two standard ways Adjacency-matrix representation
Space required O(n2). Adjacency-list representation Space required O(|E|) Depending on problems, both representations are useful.

18 Adjacency-matrix representation
Assume that the nodes are numbered 1, 2, …, n. The adjacency-matrix consists of a |V ||V | matrix A=(aij) such that if (i,j)  E aij= 1 else aij= 0. 1 2 3 4 5 2 1 5 4 3

19 Adjacency-matrix representation
Space required O(n2) The adjacency matrix for an undirected graph is symmetric. Hence, we may store only lower or upper triangle 1 2 3 4 5

20 Examples for Adjacency Matrix
1 2 1 2 3 4 5 6 2 1 3 G2 7 G1 symmetric undirected: n2/2 directed: n2 G3

21 Adjacency Matrix Analysis
Operation Time The degree of a vertex v is O(n) For a digraph (= directed graph), the row sum is the out degree, while the column sum is the in degree Find the edges incident to a given vertex v Check that two vertices v and w are adjacent O(1) Insert (or remove) a Vertex v O(n2) Insert (or remove) and Edge e

22 Adjacency-list representation
Let G=(V, E) be a graph V– set of nodes (vertices) E– set of edges. For each wV, the adjacency list A[w] contains all nodes in V that are adjacent to u. 2 5 / 1 2 1 5 4 3 2 1 5 3 4 / 3 2 4 / 4 2 5 3 / 5 4 1 2 / (a) (b)

23 Examples for Adjacency List
1 2 3 4 5 6 7 1 2 3 1 2 3 1 2 3 2 3 1 3 1 2 3 4 5 6 7 1 2 1 2 3 3 G1 1 2 1 2 5 1 2 1 4 6 2 5 7 6 G2 G3 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

24 Adjacency List Analysis
Operation Time Find the edges incident to a given vertex v O(degree(v)) Check that two vertices v and w are adjacent O(min (degree(v), degree(w))) Insert a Vertex v O(1) Remove a Vertex v Insert an Edge e

25 Example Graph in JAVA public class Graph<E> {
// 1. The vertex numbers range from 0 to labels.length-1. // 2. For each vertex number i, labels[i] contains the label for vertex i. // 3. For any two vertices i and j, edges[i][j] is true if there is a // vertex from i to j; otherwise edges[i][j] is false. private boolean[ ][ ] edges; private E[ ] labels; : // Graph methods appear here }

26 Example Graph in JAVA public Graph(int n) { edges = new boolean[n][n]; // All values initially false labels = (E[ ]) new Object[n]; // All values initially null } public void addEdge(int source, int target) edges[source][target] = true;

27 Example Graph in JAVA public Object getLabel(int vertex) { return labels[vertex]; } public boolean isEdge(int source, int target) return edges[source][target];

28 Example Graph in JAVA public int[ ] neighbors(int vertex) { int i; int count; int[ ] answer; // First count how many edges have the vertex as their source count = 0; for (i = 0; i < labels.length; i++) if (edges[vertex][i]) count++; } :

29 Example Graph in JAVA public int[ ] neighbors(int vertex) { : : // Allocate the array for the answer answer = new int[count]; // Fill the array for the answer count = 0; for (i = 0; i < labels.length; i++) { if (edges[vertex][i]) answer[count++] = i; } return answer;

30 Example Graph in JAVA public void removeEdge(int source, int target) { edges[source][target] = false; } public void setLabel(int vertex, E newLabel) labels[vertex] = newLabel; public int size( ) return labels.length;

31 Graph Traversals No obvious order of traversal (like trees)
No obvious starting point (no root) Traversals may not reach every vertex by following edges (connectedness) Traversals may return to a vertex (cycles) References: Data Structures and Algorithms in JAVA By M. Goodrich and R. Tamassia Chapters 9 & 10

32 Graph Traversals Traversal Algorithms: References:
- Depth-first search DFS. - Breadth-first search BFS. References: Data Structures and Algorithms in JAVA By M. Goodrich and R. Tamassia Chapters 9 & 10

33 Depth-first search DFS
Visits a vertex, then visit a neighbor of the vertex, visit a neighbor of the neighbor, Etc. Advance as possible from the original vertex Then back up by one vertex Considers the next neighbor

34 Example unexplored vertex visited vertex unexplored edge
B A C E A visited vertex unexplored edge discovery edge back edge D B A C E D B A C E

35 Example (cont.) D B A C E D B A C E D B A C E D B A C E

36 DFS Algorithm Algorithm DFS( v) Input A vertex v of a graph G
Output labeling of the edges of G in the connected component of v as discovery edges and back edges label v as visited for each edge e incident on v do if edge e is unexplored then let w be the other endpoint of e if vertex w is unvisited then Label e as a discovery edge recursively call DFS(w) else label e as a back edge

37 Data Structures and Algorithms in JAVA, M. Goodrich and R. Tamassia
Example 2 (a) (b) (c) (d) (e) (f) Data Structures and Algorithms in JAVA, M. Goodrich and R. Tamassia

38 Properties of DFS Property 1 Property 2
DFS(G, v) visits all the vertices in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v v D B A C E

39 Properties of DFS Property 3
Let G be a graph with n vertices and m edges. A DFS traversal of G can be performed in time O(n+m). Also, there exist O(n+m) time algorithms based on DFS for the following problems: Computing a spanning tree of G, if G is connected Computing the connected components of G Computing a path from one given vertex to another in G Computing a cycle in G, or reporting that G has no cycles

40 Breadth-first search BFS
Visits a vertex, then visit each of the vertex's neighbors before advancing

41 Note: BFS subdivides the vertices into levels (L0, L1,….etc.)
Example unexplored vertex C B A E D L0 L1 F A visited vertex A unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F Note: BFS subdivides the vertices into levels (L0, L1,….etc.)

42 Note: BFS subdivides the vertices into levels (L0, L1,….etc.)
Example C B A E D L0 L1 F C B A E D L0 L1 F L2 C B A E D L0 L1 F L2 C B A E D L0 L1 F L2 Note: BFS subdivides the vertices into levels (L0, L1,….etc.)

43 Note: BFS subdivides the vertices into levels (L0, L1,….etc.)
Example C B A E D L0 L1 F L2 L0 A L1 B C D L2 E F C B A E D L0 L1 F L2 Note: BFS subdivides the vertices into levels (L0, L1,….etc.)

44 Breadth-first search BFS
Algorithm BFS(v) Input A vertex v of a graph G Output labeling of the edges of G in the connected component of v as discovery edges and cross edges initialize container L0 to contain vertex v i  0 while Li is not empty do create container Li+1 to initially be empty for each vertex v in Li do for each edge e incident on v do if edge e is unexplored then let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge insert w into Li+1 else label e as a cross edge i  i + 1

45 Data Structures and Algorithms in JAVA, M. Goodrich and R. Tamassia
Example Data Structures and Algorithms in JAVA, M. Goodrich and R. Tamassia

46 Properties L0 L1 L2 A Property 1
BFS(s) visits all the vertices in the connected component of s Property 2 The discovery edges form a spanning tree Ts of connected component of s Property 3 For each vertex v in Li The path of Ts from s to v has i edges Any other path from s to v in Gs has at least i edges B C D E F L0 A L1 B C D L2 E F

47 Properties Property 4 Let G be a graph with n vertices and m edges. A BFS traversals of G takes time O(n+m). Also, there exist O(n+m) time algorithms based on BFS for the following problems: Testing whether G is connected Computing a spanning tree of G, if G is connected Computing the connected components of G Computing, for every vertex v of G, the minimum number of edges of any path between s and v

48 Another Implementation for BFS
Algorithm BFS(v) Input A vertex v of a graph G Output processing the vertices of the connected component of v vertexQueue = a new queue to hold vertices as they are visited Mark v as visited vertexQueue.enqueue (v) while (!vertexQueue.isEmpty ()) do frontVertex = vertexQueue.dequeue () //process frontVertex while (frontVertex has a neighbor) do nextNeighbor = next neighbor of frontVertex if (nextNeighbor is not visited) then Mark nextNeighbor as visited vertexQueue.enqueue (nextNeighbor)

49 Applications: Cycle Finding
We can specialize the DFS algorithm to find a simple cycle We use a stack S to keep track of the path between the start vertex and the current vertex As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w

50 Applications: Path Finding
Find path from source vertex s to destination vertex d Use graph search starting at s and terminating as soon as we reach d Need to remember edges traversed Use depth first search DFS? Use breath first search BFS?

51 DFS vs. BFS D C B B B A A A A G D B A F B A start DFS Process E G D C
destination D Call DFS on D C DFS on C B DFS on B B B Return to call on B A DFS on A A A A found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G G Call DFS on G D B A

52 DFS vs. BFS A B D C D G F B A start E BFS Process G D C destination
rear front rear front rear front rear front A B D C D Initial call to BFS on A Add A to queue Dequeue A Add B Dequeue B Add C, D Dequeue C Nothing to add rear front G found destination - done! Path must be stored separately Dequeue D Add G

53 Digraphs E D C B A A digraph is a graph whose edges are all directed
Short for “directed graph” Applications one-way streets flights task scheduling A graph G=(V,E) such that Each edge goes in one direction: Edge (a,b) goes from a to b, but not b to a. A C E B D

54 Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started cs21 cs22 cs23 cs51 cs53 cs52 cs181 cs131 cs141 cs121 cs171 The good life cs151

55 Reachability Directed Graph rooted at v : vertices reachable from v via directed paths v E D v E D C A C F v A B E D C F A B

56 Strong Connectivity Each vertex can reach all other vertices a g c d e
b e f g

57 Strong Connectivity Algorithm
Pick a vertex v in G. Perform a DFS from v in G. If there’s a w not visited, print “no”. Let G’ be G with edges reversed. Perform a DFS from v in G’. Else, print “yes”. Running time: O(n+m). c d e b f a G’: g c d e b f

58 Transitive Closure Given a digraph G, the transitive closure of G is the digraph G* such that G* has the same vertices as G if G has a directed path from u to v (u  v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph B A D C E G B A D C E G*

59 Computing the Transitive Closure
We can perform DFS starting at each vertex Based on the idea that, if there's a way to get from A to B and from B to C, then there's a way to get from A to C. O(n(n+m)) Alternatively ... Use The Floyd-Warshall Algorithm

60 Floyd-Warshall Transitive Closure
Floyd-Warshall’s algorithm numbers the vertices of G as v1 , …, vn and computes a the transitive closure in a series of rounds Initialize G0=G Begin a computation of the rounds, beginning with round 1 In generic round k , we construct Gk as: Gk = Gk-1 Add a directed edge (vi, vj) to Gk , if Gk-1 contains both the edges (vi, vk) and (vk, vj) After n rounds, we have that Gn = G* Running time: O(n3), assuming adjacency matrix representation (i.e. checking whether 2 vertices are adjacent is O(1) )

61 Floyd-Warshall - Example
JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6 G0=G

62 Floyd-Warshall - Iteration 1
G1 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

63 Floyd-Warshall - Iteration 2
G2 JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6

64 Floyd-Warshall - Iteration 3
G3 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v v 3 1 MIA v 5

65 Floyd-Warshall - Iteration 4
G4 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v v 3 1 MIA v 5

66 Floyd-Warshall - Iteration 5
G5 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

67 Floyd-Warshall - Iteration 6
JFK MIA ORD LAX DFW SFO v 2 1 3 4 5 6 BOS G6

68 Floyd-Warshall - Conclusion
JFK MIA ORD LAX DFW SFO v 2 1 3 4 5 6 BOS G7

69 Floyd-Warshall - Algorithm
Algorithm FloydWarshall(G) Input digraph G with n vertices Output transitive closure G* of G i := 1 for all v  G denote v as vi i := i + 1 G0 := G for k := 1 to n do Gk := Gk - 1 for i := 1 to n (i  k) do for j := 1 to n (j  i, k) do if both edges (vi, vk) and (vk, vj)  Gk - 1 then add edge (vi, vj) to Gk. (if it is not already present) return Gn

70 DAGs and Topological Ordering
A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v1 , …, vn of the vertices such that for every edge (vi , vj), we have i < j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem A digraph might has a topological ordering if and only if it is a DAG B A D C E DAG G Topological ordering of G v1 v2 v3 v4 v5

71 Topological Sorting Number vertices, so that (u,v) in G implies u < v 1 wake up 2 3 eat study computer sci. A typical student day 4 5 nap more c.s. 7 play 8 write c.s. program 6 9 work out make cookies for professors 10 sleep 11 dream about graphs

72 Algorithm for Topological Sorting
Algorithm TopologicalSort(G) H := G // Temporary copy of G n := the number of vertices in G while H is not empty do Let v be a vertex with no outgoing edges Label v := n n := n - 1 Remove v from H Running time: O(n + m)

73 Topological Sorting Example

74 Topological Sorting Example
9

75 Topological Sorting Example
9

76 Topological Sorting Example
8 9

77 Topological Sorting Example
8 9

78 Topological Sorting Example
7 8 9

79 Topological Sorting Example
7 8 9

80 Topological Sorting Example
6 7 8 9

81 Topological Sorting Example
6 7 8 9

82 Topological Sorting Example
6 5 7 8 9

83 Topological Sorting Example
6 5 7 8 9

84 Topological Sorting Example
4 6 5 7 8 9

85 Topological Sorting Example
4 6 5 7 8 9

86 Topological Sorting Example
3 4 6 5 7 8 9

87 Topological Sorting Example
3 4 6 5 7 8 9

88 Topological Sorting Example
2 1 3 4 6 5 7 8 9

89 Topological Sorting Example
2 1 3 4 6 5 7 8 9


Download ppt "Graphs."

Similar presentations


Ads by Google