Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),

Similar presentations


Presentation on theme: "What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),"— Presentation transcript:

1 What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: a b V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)} c d e

2 A graph consists G of a set V of elements called nodes ( points or vertices ) a set E of edges such that each e in E is identified with unique unordered pair [u , v] of nodes in v and is denoted by e = [u , v] G = ( V , E ) If e = [ u , v ] then end points of e : u , v Adjacent nodes or neighbor nodes : u , v degree of a node : number of edges in node isolated node : if deg(u) = 0 Path : path p of length n from node u to node v is a sequence of n+1 nodes ( v0, v1, v2, , vn) such that u = v0 and v = vn vi-1 is adjacent to vi for I = 1 to n Closed Path : if v0 = vn Simple Path : If all nodes in a path are distinct Cycle : closed path with length 3 or more

3 K - Cycle : cycle of length k
Connected Graph : If there is a path between any two of its node Complete graph : If every node u in G is adjacent to every other node v in G. A complete graph will have n (n-1) /2 edges Tree graph of free tree : Connected Graph without cycle Labeled graphs : if edges are assigned data Weights : non negative numeric value, may be Length of the edge. If no weight is specified then w(e) = 1 is considered Weight of the Path : Sum of weights of all edges in this path Multiple Edges : e1 and e2 are multiple edges if both e1 and e2 = [u , v] Loops : is it has identical end points, e = [ u , u] Multigraph : with multiple edges and loops Finite multigraph : with finite number of edges and nodes

4 Directed Graph or Digraph : if edges have direction
edge is identified by ordered pair of nodes ( u , v ) Arc : Directed Edge Different terminology used for end points : Begin End, Origin and terminal point, predecessor and successor Outdegree of node u : number of edges beginning at u Indegree of node u : number of edges ending at u Source node: +ve outdegree and 0 indegree Sink : +ve indegree and 0 outdegree A node v is reachable from node u if there is a directed path from v to u Strongly connected directed graph : if for each pair of node there is a path from u to v and v to u Unilaterally connected directed graph : if for each pair of node there is a path from u to v or v to u Parallel Edges, simple directed graph

5 Applications electronic circuits CS16
networks (roads, flights, communications) LAX JFK DFW STL HNL FTL

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

7 Terminology: 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: 3 2 1 2 1 2 3 3 3 3 3 4 5 6 3 G1 3 1 1 1 1 G2 directed graph 1 in: 1, out: 2 in-degree out-degree in:1, out: 1 2 in: 1, out: 0 G3

9 Terminology: Path path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. 3 2 3 3 3 a b a b c c d e d e a b e d c b e d c

10 More Terminology simple path: no repeated vertices
cycle: simple path, except that the last vertex is the same as the first vertex a b b e c c d e

11 Even More Terminology connected graph: any two vertices are connected by some path connected not connected subgraph: subset of vertices and edges forming a graph connected component: maximal connected subgraph. E.g., the graph below has 3 connected components.

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

13 More… tree - connected graph without cycles
forest - collection of trees

14 Connectivity Let n = #vertices, and m = #edges
A complete graph: one 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. Therefore, if a graph is not complete, m < n(n -1)/2

15 More Connectivity n = #vertices m = #edges For a tree m = n - 1
If m < n - 1, G is not connected

16 Oriented (Directed) Graph
A graph where edges are directed

17 Directed vs. Undirected Graph
An undirected graph is one in which the pair of vertices in a edge is unordered, (v0, v1) = (v1,v0) A directed graph is one in which each edge is a directed pair of vertices, <v0, v1> != <v1,v0> tail head

18 Adjacency Matrix Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional n by n array, say adj_mat If the edge (vi, vj) is in E(G), adj_mat[i][j]=1 If there is no such edge in E(G), adj_mat[i][j]=0 The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric

19 Examples for Adjacency Matrix
1 2 3 4 5 6 7 1 2 3 1 2 G2 G1 symmetric G4

20 Merits of Adjacency Matrix
From the adjacency matrix, to determine the connection of vertices is easy The degree of a vertex is For a digraph (= directed graph), the row sum is the out-degree, while the column sum is the in-degree

21 Linked representation(Adjacency List)
Each row in adjacency matrix is represented as an adjacency list. 1 2 1 3 1 2 3 2 1 2 3 2 3 1 2 1 1 3 2 1 2 G1 G2

22 Sequential Representation of Graphs : Adjacency Matrix and Path Matrix
W Y Z 1 1 2 1 2 1 2 3 A3 = A1 = A2 = A4 = Number of Paths between Y to W of length 3 are 2 (Y -> Z -> X -> W AND Y->W->Z->W) B4 = A1 + A2 + A3 + A4 entry of matrix B4 gives the number of paths of length 4 or less from node vi to vj entry of matrix Br gives the number of paths of length r or less from node vi to vj

23 Pij = 1 (if there is path from vi to vj) = 0 (otherwise)
Path matrix: Let G be simple directed graph with m nodes (v1, v2, vm ). Then, path matrix or reach-ability matrix of G is: Pij = 1 (if there is path from vi to vj) = 0 (otherwise) B = A1 + A2 + A3 + A4 Gives path matrix P when all non-zero entries are replaced by 1 1 P =

24 Example of transitive closure:
Transitive closure: For a graph G, it is defined to be graph G’ such that it has same nodes as G & there is an edge (vi, vj) in G’ whenever there is path from vi to vj in G. Example of transitive closure: Initial graph Final graph 3 4 2 1 3 4 2 1

25 Warshall’s Algorithm It is used to find path matrix in efficient way, rather than calculating powers of adjacency matrix. First we define m-square Boolean matrices P0, P1, Pm as follows. Let Pk [i, j] denote the ij entry of matrix Pk. Then, we define: Pk [i, j] = 1 (if there is simple path from vi to vj which use nodes from v1, v2, vk ). = 0 (otherwise)

26 Warshall’s Algorithm Warshall observed that Pk [i, j] = 1 can occur in following 2 cases: There is simple path from vi to vj which only used nodes from v1, v2, vk-1 , hence Pk-1 [i, j] = 1 There is simple path from vi to vk and from vk to vj which only used nodes from v1, v2, vk-1 , hence Pk-1 [i, k] = and Pk-1 [k, j] = 1 Accordingly: Pk [i, j] = Pk-1 [i, j] v (Pk-1 [i, k] ^ Pk-1 [k, j] )

27 Finding transitive closure
3 4 2 1 3 4 2 1 R2 3 4 2 1 R1 R0 3 4 2 1 R4 R3 3 4 2 1

28 Warshall’s Algorithm A directed graph G with M nodes is maintained in memory by its adjacency matrix A. This algo. finds path matrix P of graph G. Repeat for i, j = 1, 2, M : //[Initializes P] If A[i,j] = 0 then Set P[i,j] = 0 else Set P[i,j] = 1 2. Repeat Steps 3 and 4 for k = 1, 2, M //[updates P] Repeat Step 4 for i = 1, 2, M Repeat for j = 1, 2, M Set P[i,j] = P[i,j] v (P[i,k] ^ P[k,j]) 3. Exit

29 Shortest Path (Modified Warshall’s algorithm)
7 5 12 2 3 4 9 1 7 5 2 3 4 1 Q1 = Q0 = 7 5 12 2 10 3 4 1 6 7 5 8 6 3 2 9 4 1 7 5 12 2 10 3 4 9 1 11 Q3 = Q4 = Q2 =

30 Shortest Path (Modified Warshall’s algorithm)
Graph G is maintained in memory by its weight matrix W = wij defined as: wij = w(e) (if there is an edge from vi to vj) = 0 (otherwise) Therefore, we define matrix Q by modifying Warshall’s algo. as: Qk [i,j] = the smaller of the length of preceding path from vi to vj or = the sum of lengths of preceding paths from vi to vk and from vk to vj Accordingly: Qk [i, j] = Min (Qk-1 [i, j] , Qk-1 [i, k] + Qk-1 [k, j] )

31 Shortest Path (Modified Warshall’s algorithm)
A weighted graph G with M nodes is maintained in memory by its weight matrix W. This algo. finds matrix Q such that Q[i,j] is length of shortest path from node vi to vj. Repeat for i, j = 1, 2, M //[Initializes Q] if W[i,j] = 0 then Set Q[i,j] = Infinity else Set Q[i,j] = W[i,j] 2. Repeat Steps 3 and 4 for k = 1, 2, M //[updates Q] Repeat Step 4 for i = 1, 2, M Repeat for j = 1, 2, M Set Q[i,j] = Min (Q[i,j], Q[i,k] + Q[k,j] ) 3. Exit

32 Traversing a Graph Systematically examining the nodes and edges of a graph. Two methods Breadth First Search (BFS) Depth First Search (DFS) BFS will use Queue and DFS will use Stack as auxiliary storage The algorithm will process only those nodes which are reachable from the starting node During the execution of the algorithm, each node N of G will be in one of the three states, called status of N STATUS = 1 : Ready State , i.e. initial state of node N STATUS = 2 : Waiting State , i.e. node N is in Queue or stack waiting to be processed STATUS = 3 : Processed State , i.e. node N has been processed

33 Breadth-First Search:
Initialize all nodes to ready state , STATUS = 1 Put the starting node A in Queue and change its status to waiting, STATUS = 2 Repeat step 4 and 5 until Queue is empty Remove the front node N of Queue. Process N and change STATUS of N to processed, STATUS = 3 Add to the rear of Queue all neighbors of N that are in ready state (STATUS = 1) and change their STATUS to waiting state ( STATUS = 2 ) Exit

34

35 STEP QUEUE ORIGIN F R 1 A 2 AFCB 0AAA 4 3 AFCBD 0AAAF 5 AFCBDG 0AAAFB
Node Adj List A F, C , B B G, C C F D E D, C, J G C, E J D, K K E, G STEP QUEUE ORIGIN F R 1 A 2 AFCB 0AAA 4 3 AFCBD 0AAAF 5 AFCBDG 0AAAFB 6 7 AFCBDGE 0AAAFBG 8 AFCBDGEJ 0AAAFBGJ 9 .. 10 ….

36 Applications of Breadth First Search
Shortest Path and Minimum Spanning Tree for un-weighted graph Peer to Peer Networks. Crawlers in Search Engines Social Networking Websites GPS Navigation systems Broadcasting in Network In Garbage Collection

37 Depth-First Search: Initialize all nodes to ready state , STATUS = 1
Push the starting node A in STACK and change its status to waiting, STATUS = 2 Repeat step 4 and 5 until STACK is empty Pop the TOP node N of STACK. Process N and change STATUS of N to processed, STATUS = 3 Push onto STACK all neighbors of N that are in ready state (STATUS = 1) and change their STATUS to waiting state ( STATUS = 2 ) Exit

38 Iterative Recursive

39 1 A 2 FCB AB 3 FCG ABG 4 FCE ABGE 5 FCDJ ABGEJ 6 … Node Adj List A
G, C C F D E D, C, J G C, E J D, K K E, G STEP STACK POP 1 A 2 FCB AB 3 FCG ABG 4 FCE ABGE 5 FCDJ ABGEJ 6

40 Applications of Depth First Search
Finding minimum spanning tree Detecting cycle in a graph Path Finding Topological Sorting Solving puzzles with only one solution

41 Linked Representation of Graph

42 A B C D E Node Adjacency List A B, C, D B C D C, E E

43

44 Sequential representation of graph G in memory using adjacency matrix A has some drawbacks.
It is difficult to insert/delete nodes in G (since size of A needs to be changed & nodes to be reordered). Matrix A may be sparse (containing many zeros for no edges) in which great deal of memory is wasted.

45 Operation on Graph Searching: Find(info, link, start, item, loc)
Finds the location loc of first node containing item, or sets loc = NULL Set Ptr = start Repeat while Ptr != NULL If item = info[Ptr], then Set loc = ptr and return else Set Ptr = link[Ptr] 3. Set loc = NULL and return

46 Delete(info, link, start, avail, item, flag)
Deleting: Delete(info, link, start, avail, item, flag) Deletes first node in list containing item, or sets flag=False when item doesn’t appear in list. [List empty?] If start = NULL, then Set flag = False and return [item in first node?] If info[start] = item, then Set Ptr = start, start = link[start] link[Ptr] = avail, avail = Ptr flag = true and return Set Ptr = link[start] and save = start [initializes pointers] Repeat steps 5 and 6 while Ptr != NULL If Info[Ptr] = item, then Set link[save] = link[Ptr], link[Ptr] = avail avail = Ptr, flag = True and return Set save = Ptr and Ptr = link[Ptr] [updates pointers] Set flag = False and return

47 InsNode(node, next, Adj, start, availN, N, flag)
Inserting: InsNode(node, next, Adj, start, availN, N, flag) This procedure inserts node N in graph G. [Overflow?] If availN = NULL, then Set flag = False and return Set Adj[availN] = NULL [node is isolated] [Removes node from availN list] Set New = availN and availN = next[availN] [Inserts node N in node list] Set node[New] = N, next[New] = start, start = New 5. Set flag = True and return.


Download ppt "What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),"

Similar presentations


Ads by Google