Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 7 Visit for more Learning Resources.

Similar presentations


Presentation on theme: "Graphs Chapter 7 Visit for more Learning Resources."— Presentation transcript:

1 Graphs Chapter 7 Visit for more Learning Resources

2 What is a graph? A data structure that consists of a set of finite nodes (V) and a set of edges(E) that relate the nodes to each other G=(V,E) V(G)=Vertices of graph G ,e.g. {1,2,3,4} E(G)=Edges of graph G ,e.g.{(1,2),(1,3),(3,4)} 1 4 2 3

3 Graphs u v Vertices are also called nodes and points.
E is the edge set. Each edge connects two different vertices. Edges are also called arcs and lines. Directed edge has an orientation (u,v). u v

4 Graphs Undirected graph => no oriented edge.
Undirected edge has no orientation (u,v). u v Undirected graph => no oriented edge. Directed graph => every edge has an orientation.

5 Undirected Graph 2 3 8 10 1 4 5 9 11 6 7

6 Directed Graph (Digraph)
2 3 8 10 1 4 5 9 11 6 7

7 Applications—Communication Network
2 3 8 10 1 4 5 9 11 6 7 Internet connection. Vertices are computers. Send from 1 to 7. Vertex = city, edge = communication link.

8 Driving Distance/Time Map
2 3 8 10 1 4 5 9 11 6 7 Vertex = city, edge weight = driving distance/time.

9 Street Map 2 3 8 10 1 4 5 9 11 6 7 Some streets are one way.

10 Complete Undirected Graph
Has all possible edges. n = 2 n = 4 n = 3 n = 1

11 Number Of Edges—Undirected Graph
Each edge is of the form (u,v) Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is the same as edge (v,u), the number of edges in a complete undirected graph is n(n-1)/2. Number of edges in an undirected graph is <= n(n-1)/2.

12 Number Of Edges—Directed Graph
Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is not the same as edge (v,u), the number of edges in a complete directed graph is n(n-1). Number of edges in a directed graph is <= n(n-1).

13 Graph terminology 1.Adjacent nodes: two nodes are adjacent if they are connected by an edge 2.Path: A sequence of edges that connect two nodes in a graph 3.Path Length: Total no of edges included in path. 4.Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5

14 Graph terminology (cont.)
What is the number of edges in a complete directed graph with N vertices?  N * (N-1)

15 Graph terminology (cont.)
What is the number of edges in a complete undirected graph with N vertices?  N * (N-1) / 2

16 5. Vertex Degree OR Degree of Node
2 3 8 10 1 4 5 9 11 6 7 Number of edges incident to vertex. degree(2) = 2, degree(5) = 3, degree(3) = 1

17 6.In-Degree Of A Vertex 2 3 8 10 1 4 5 9 11 6 7 in-degree is number of incoming edges indegree(2) = 1, indegree(8) = 0

18 7.Out-Degree Of A Vertex 2 3 8 10 1 4 5 9 11 6 7 out-degree is number of outbound edges outdegree(2) = 1, outdegree(8) = 2

19 Graph terminology (cont.)
8.Source:A Node which has only outgoing edges. 9.Sink: A Node which has only Incoming edges. 10. Pendant Node: if indegree of node is 1 and outdegree is 0 then it is pendant Node. 11. Reachable: If path exist between two nodes then they are reachable.

20 Graph terminology (cont.)
12. Sling or loop: An edge of graph which joins a node to itself is called sling or loop. 13.Parallel Edges: The two distinct edges between a pair to nodes which are opposite in a direction. 14.Weighted Edge: edge has Numerical Values. 15.Isolated Node: Not an Adjacent node of any other node

21 Graph terminology (cont.)
16. Weighted graph: a graph in which each edge carries a value 2 3 8 10 1 4 5 9 11 6 7

22 Graph terminology (cont.)
17. Isolated Graph: A Graph which contain only isolated node. 18. Directed Acyclic Graph: A diagraph with no cycles. 2 1 4 5

23 Graph implementation Array-based implementation
A 1D array is used to represent the vertices A 2D array (adjacency matrix) is used to represent the edges

24 Graph Representation There are two ways to represent the Graph:
Sequential Representation Adjacency Matrix Path Matrix Adjacency Lists/Linked List representation.

25 1.Adjacency Matrix 0/1 n x n matrix, where n = # of vertices A(i,j) = 1 iff (i,j) is an edge 2 3 1 4 5 1 2 3 4 5 1 1 1 1 1 1 1 1 1 1

26 Path Matrix

27 Path Matrix

28

29 Adjacency Lists aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5)
Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists. aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) aList[4] = (5,1) aList[5] = (2,4,3) 2 3 1 4 5

30 Adjacency Lists Representation
A graph of n nodes is represented by a one-dimensional array L of linked lists, where L[i] is the linked list containing all the nodes adjacent from node i. The nodes in the list L[i] are in no particular order

31 Example of List Helen 1 Mary 0 Joe 3 John 2 Tom 4 Paul 5
L[0]: empty L[1]: empty L[2]: 0, 1, 4, 5 L[3]: 0, 1, 4, 5 L[4]: 0, 1 L[5]: 0, 1 Helen 1 Mary 0 Joe 3 John 2 Tom 4 Paul 5

32 Linked Adjacency Lists
Each adjacency list is a chain. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4] 2 4 1 5 5 5 1 2 4 3 Array length n simply means we need an array with n spots. A direct implementation using a Java array would need n+1 spots, because spot 0 would not be utilized. However, by using spot 0 for vertex 1, spot 1 for vertex 2, and so on, we could get by with a Java array whose length is actually n. Array Length = n # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph)

33 Array Adjacency Lists aList[1] [2] [3] [4] aList[5] Array Length = n
Each adjacency list is an array list. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4] Array Length = n # of list elements = 2e (undirected graph) # of list elements = e (digraph)

34 Graph Search (traversal)
How do we search a graph? At a particular vertices, where shall we go next? Two common Methods: Breadth-first search (BFS) Depth-first search (DFS) In BFS, one explore a graph level by level away (explore all neighbors first and then move on) In DFS, go as far as possible along a single path until reach a dead end (a vertex with no edge out or no neighbor unexplored) then backtrack Data Structure and Algorithm

35 A vertex u is reachable from vertex v iff there is a path from v to u.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. 2 3 8 10 1 4 5 9 11 6 7

36 Graph Search Methods A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v. 2 3 8 10 1 4 5 9 11 6 7 Visit, mark, and label used as synonyms.

37 Breadth-First Search Visit start vertex and put into a FIFO queue.
Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue.

38 Breadth-First Search Example
2 3 8 10 1 4 5 9 11 6 7 Start search at vertex 1.

39 Breadth-First Search Example
FIFO Queue 1 2 3 8 1 1 4 5 9 10 6 7 11 Visit/mark/label start vertex and put in a FIFO queue.

40 Breadth-First Search Example
FIFO Queue 1 2 3 8 1 1 4 5 9 10 6 7 11 Remove 1 from Q; visit adjacent unvisited vertices; put in Q.

41 Breadth-First Search Example
2 FIFO Queue 2 3 2 4 8 1 1 4 4 5 9 10 6 7 11 Remove 1 from Q; visit adjacent unvisited vertices; put in Q.

42 Breadth-First Search Example
2 FIFO Queue 2 3 2 4 8 1 1 4 4 5 9 10 6 7 11 Remove 2 from Q; visit adjacent unvisited vertices; put in Q.

43 Breadth-First Search Example
2 FIFO Queue 2 3 3 4 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 2 from Q; visit adjacent unvisited vertices; put in Q.

44 Breadth-First Search Example
2 FIFO Queue 2 3 3 4 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 4 from Q; visit adjacent unvisited vertices; put in Q.

45 Breadth-First Search Example
2 FIFO Queue 2 3 3 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 4 from Q; visit adjacent unvisited vertices; put in Q.

46 Breadth-First Search Example
2 FIFO Queue 2 3 3 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 5 from Q; visit adjacent unvisited vertices; put in Q.

47 Breadth-First Search Example
2 FIFO Queue 2 3 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 5 from Q; visit adjacent unvisited vertices; put in Q.

48 Breadth-First Search Example
2 FIFO Queue 2 3 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 3 from Q; visit adjacent unvisited vertices; put in Q.

49 Breadth-First Search Example
2 FIFO Queue 2 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 3 from Q; visit adjacent unvisited vertices; put in Q.

50 Breadth-First Search Example
2 FIFO Queue 2 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 6 from Q; visit adjacent unvisited vertices; put in Q.

51 Breadth-First Search Example
2 FIFO Queue 2 3 3 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 6 from Q; visit adjacent unvisited vertices; put in Q.

52 Breadth-First Search Example
2 FIFO Queue 2 3 3 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 9 from Q; visit adjacent unvisited vertices; put in Q.

53 Breadth-First Search Example
2 FIFO Queue 2 3 3 7 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 9 from Q; visit adjacent unvisited vertices; put in Q.

54 Breadth-First Search Example
2 FIFO Queue 2 3 3 7 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 7 from Q; visit adjacent unvisited vertices; put in Q.

55 Breadth-First Search Example
2 FIFO Queue 2 3 3 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 7 from Q; visit adjacent unvisited vertices; put in Q.

56 Breadth-First Search Example
2 FIFO Queue 2 3 3 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 8 from Q; visit adjacent unvisited vertices; put in Q.

57 Breadth-First Search Example
2 FIFO Queue 2 3 3 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Queue is empty. Search terminates.

58 Breadth-First Search Property
All vertices reachable from the start vertex (including the start vertex) are visited.

59 Breadth-first Search (BFS)
Search for all vertices that are directly reachable from the root (called level 1 vertices) After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on. Data Structure and Algorithm

60 Example r s t u v w x y Data Structure and Algorithm

61 Example        Q: s r s t u r s t u v w x y v w x y
v w x y v w x y Q: s Data Structure and Algorithm

62 Example 1    1   Q: w r r r s s t t u u v v w w x x y y
1 v v w w x x y y Q: w r Data Structure and Algorithm

63 Example 1 2   1 2  Q: r t x r s t u r s t u v w x y v w x y
2 1 2 v w x y v w x y Q: r t x Data Structure and Algorithm

64 Example 1 2  2 1 2  Q: t x v r s t u r s t u v w x y v w x y
2 2 1 2 v w x y v w x y Q: t x v Data Structure and Algorithm

65 Example 1 2 3 2 1 2  Q: x v u r s t u r s t u v w x y v w x y
2 3 2 1 2 v w x y v w x y Q: x v u Data Structure and Algorithm

66 Example 1 2 3 2 1 2 3 Q: v u y r s t u r s t u v w x y v w x y
2 3 2 1 2 3 v w x y v w x y Q: v u y Data Structure and Algorithm

67 Example 1 2 3 2 1 2 3 Q: u y r s t u r s t u v w x y v w x y
2 3 2 1 2 3 v w x y v w x y Q: u y Data Structure and Algorithm

68 Example 1 2 3 2 1 2 3 Q: y r s t u r s t u v w x y v w x y
2 3 2 1 2 3 v w x y v w x y Q: y Data Structure and Algorithm

69 All Vertices are Visited……..
Example r s t u r s t u 1 2 3 2 1 2 3 v w x y v w x y All Vertices are Visited…….. Q: Ø Data Structure and Algorithm

70 Depth-First Search (DFS)
The basic idea behind this algorithm is that it traverses the graph using recursion Go as far as possible until you reach a dead end Backtrack to the previous path and try the next branch The graph below, started at node a, would be visited in the following order: a, b, c, g, h, i, e, d, f, j d a b h i c g e j f Data Structure and Algorithm

71 Depth-First Search depthFirstSearch(v) { Label vertex v as reached. for (each unreached vertex u adjacenct from v) depthFirstSearch(u); } Note that vertices adjacent from v are examined one at a time. As soon as an unreached adjacent vertex u is found, a depthFirstSearch(u) is done. Remaining vertices adjacent from v are examined after depthFirstSearch(u) completes.

72 Depth-First Search Example
2 3 8 10 1 4 5 9 11 6 7 2 1 Start search at vertex 1. Label vertex 1 and do a depth first search from either 2 or 4. Suppose that vertex 2 is selected.

73 Depth-First Search Example
2 2 2 3 8 1 1 4 5 5 9 10 6 7 11 Label vertex 2 and do a depth first search from either 3, 5, or 6. Suppose that vertex 5 is selected.

74 Depth-First Search Example
2 2 2 3 8 1 1 4 5 5 5 9 9 10 6 7 11 Label vertex 5 and do a depth first search from either 3, 7, or 9. Suppose that vertex 9 is selected.

75 Depth-First Search Example
2 2 2 3 8 8 1 1 4 5 5 5 9 9 9 10 6 7 11 Label vertex 9 and do a depth first search from either 6 or 8. Suppose that vertex 8 is selected.

76 Depth-First Search Example
2 2 2 3 8 8 8 1 1 4 5 5 5 9 9 9 10 6 6 7 11 Label vertex 8 and return to vertex 9. From vertex 9 do a dfs(6).

77 Depth-First Search Example
2 2 2 3 8 8 8 1 1 4 4 5 5 5 9 9 9 10 6 6 6 7 11 Label vertex 6 and do a depth first search from either 4 or 7. Suppose that vertex 4 is selected.

78 Depth-First Search Example
2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 11 Label vertex 4 and return to 6. From vertex 6 do a dfs(7).

79 Depth-First Search Example
2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Label vertex 7 and return to 6. Return to 9.

80 Depth-First Search Example
2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to 5.

81 Depth-First Search Example
2 2 2 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Do a dfs(3).

82 Depth-First Search Example
2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Label 3 and return to 5. Return to 2.

83 Depth-First Search Example
2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to 1.

84 Depth-First Search Example
2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to invoking method. For more detail contact us


Download ppt "Graphs Chapter 7 Visit for more Learning Resources."

Similar presentations


Ads by Google