Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals

Similar presentations


Presentation on theme: "Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals"— Presentation transcript:

1 Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
College of Computer Science & Engineering Information & Computer Science Department Unit 10 Graphs (1)

2 Reading Assignment “Data Structures and Algorithms in Java”, 3rd Edition, Adam Drozdek, Cengage Learning, ISBN Chapter 8 Section 8.1: Graph Representations Section 8.2: Graph Traversals Section 8.7: Topological Sort

3 Outline Introduction to Graphs Graph Traversals Topological Sort

4 Introduction to Graphs
What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. Adjacency Matrix. Adjacency Lists. Simple Lists Incidence Matrix Review Questions.

5 What is a Graph? Graphs are Generalization of Trees.
A simple graph G = (V, E) consists of a non-empty set V, whose members are called the vertices of G, and a set E of pairs of distinct vertices from V, called the edges of G. A simple graph has no loop (An edge that connects a vertex to itself) A simple graph has no vertices joined by multiple edges Undirected Directed (Digraph) Weighted

6 Some Example Applications of Graph
Finding the least congested route between two phones, given connections between switching stations. Finding the shortest path from one city to another. As a traveling sales-person, finding the cheapest path that passes through all the cities that the sales person must visit. Determining an ordering of courses so that prerequisite courses are always taken first. Networks: Vertices represent computers and edges represent network connections between them. World Wide Web: Vertices represent webpages, and edges represent hyperlinks. Determining if there is a way to get from one page to another, just by following links.

7 Graphs Terminologies Path Cycle Connected Disconnected
Adjacent Vertices: there is a connecting edge. A Path: A sequence of adjacent vertices. A Cycle: A path in which the last and first vertices are adjacent. Connected graph: There is a path from any vertex to every other vertex. Path Cycle Connected Disconnected

8 More Graph Terminologies
Path and cycles in a digraph: must move in the direction specified by the arrow. Connectedness in a digraph: strong and weak. Strongly Connected: If connected as a digraph - following the arrows. Weakly connected: If the underlying undirected graph is connected (i.e. ignoring the arrows). Directed Cycle Strongly Connected Weakly Connected

9 Further Graph Terminologies
Emanate: an edge e = (v, w) is said to emanate from v. A(v) denotes the set of all edges emanating from v. Incident: an edge e = (v, w) is said to be incident to w. I(w) denote the set of all edges incident to w. Out-degree: number of edges emanating from v -- |A(v)| In-degree: number of edges incident to w -- |I(w)|. Directed Graph Undirected Graph

10 Further Graph Terminologies
Subgraph: A graph G = (VG , EG ) is a subgraph of H = (VH, EH) if VG  VH and EG  EH. A spanning subgraph of G is a subgraph that contains all the vertices of G A spanning tree of a connected graph is a spanning subgraph that is a tree

11 Graph Representations
For vertices: an array or a linked list can be used For edges: Adjacency Matrix (Two-dimensional array) Adjacency List (One-dimensional array of linked lists) Linked List (one list only) Incidence Matrix (Two-dimensional array)

12 Adjacency Matrix Representation
Adjacency Matrix uses a 2-D array of dimension |V|x|V| for edges. (For vertices, a 1-D array is used) The presence or absence of an edge, (v, w) is indicated by the entry in row v, column w of the matrix. For an unweighted graph, boolean values could be used. For a weighted graph, the actual weights are used.

13 Notes on Adjacency Matrix
For undirected graph, the adjacency matrix is always symmetric. In a Simple Graph, all diagonal elements are zero (i.e. no edge from a vertex to itself). The space requirement of adjacency matrix is O(n2) - most of it wasted for a graph with few edges. However, entries in the matrix can be accessed directly.

14 Adjacency List Representation
This involves representing the set of vertices adjacent to each vertex as a list. Thus, generating a set of lists. This can be implemented in different ways. Our representation: Vertices as a one dimensional array Edges as an array of linked list (the emanating edges of vertex 1 will be in the list of the first element, and so on, … vertices edges 1 2 3 4 1,2 1,3 Null 2,3 2,4 Null Empty 4,1 4,2 4,3 Null

15 Simple List Representation
Vertices are represented as a 1-D array or a linked list Edges are represented as one linked list Each edge contains the information about its two vertices vertices edges 1 2 3 4 edge(1,2) edge(1,3) edge(2,3) edge(2,4) edge(4,1) edge(4,2) edge(4,3) . .

16 Incidence Matrix Representation
Each row represents a vertex and each column represent an edge (|V|  |E|) Matrix Entries of the matrix can be 0-1

17 Comparison between the Adjacency List and the Adjacency Matrix Representation
For a Graph G(V, E), let |V| = n and |E| = m – Is there an edge from x to y? • Matrix: O(1) check value at (x, y) • List: O(n) index to x, traverse list to y or end – Edge insertion or deletion Matrix: O(1) List: O(n) - Get successor vertices of a vertex. • Matrix: O(n) scan a row • List: O(n) traverse a linked list – Get predecessor vertices of a vertex. • Matrix: O(n) scan a column • List: O(n + m) traverse all linked lists, which could be as bad as O(n+n2) = O(n2). Visit all edges Matrix: O(n2) List: O(n + m) Space List: O(n + m)

18 Review Questions Consider the undirected graph GA shown above. List the elements of V and E. Then, for each vertex v in V, do the following: Compute the in-degree of v Compute the out-degree of v List the elements of A(v) List the elements of I(v). Consider the undirected graph GA shown above. Show how the graph is represented using adjacency matrix. Show how the graph is represented using adjacency lists. Repeat Exercises 1 and 2 for the directed graph GB shown above.

19 Graph Traversals General Traversal Algorithm Depth-First Traversals.
Algorithms. Example. Implementation. Breadth-First Traversal. The Algorithm. Review Questions.

20 General Traversal Algorithm
This algorithm ensures that all vertices are visited in the graph. This is certainly important in the case where the graph is disconnected (in the case of undirected graphs) or not strongly connected (in the case of directed graphs) The method call doTraverse is replaced with all the subsequent traversal methods discussed. dfsPreorder, dfsPostorder, BreadthFirst. GraphTraversal (Graph G) for each vertex v  G do mark v as unvisited; if v is marked as unvisited doTraverse(G, v);

21 Depth-First Traversal Algorithm
In this method, After visiting a vertex v, which is adjacent to w1, w2, w3, ...; Next we visit one of v's adjacent vertices, w1 say. Next, we visit all vertices adjacent to w1 before coming back to w2, etc. Must keep track of vertices already visited to avoid cycles. The method can be implemented using recursion or iteration. The iterative preorder depth-first algorithm is: push the starting vertex, v, onto the stack; while(stack is not empty) { pop vertex v off the stack; if v is not visited, visit v and mark it as visited; for each vertex w adjacent to v that is not marked visited do { push w onto the stack;} } Note: Adjacent vertices can be pushed in any order; but to obtain a unique traversal, we will push them in reverse alphabetical order.

22 Example Demonstrates depth-first traversal using an explicit stack.
Order of Traversal A B C F E G D H I Stack

23 Recursive Preorder Depth-First Traversal Algorithm
dfsPreorder(G, v){ visit v; mark v as visited ; for each adjacent vertex w of v do if (w has not been marked as visited) { dfsPreorder(G, w); }

24 Recursive Preorder Depth-First Traversal (Example)
The Preorder Depth First Traversal Tree is shown below: A B C D E F G H I

25 Recursive Postorder Depth-First Traversal Algorithm
dfsPostorder(G, v){ mark v as visited; for(each neighbour w of v) if(w is not marked visited) dfsPostorder(G, w); visit v; }

26 Recursive postorder Depth-First Traversal (Example)
The Postorder Depth First Traversal Tree is shown below: A B C D E F G H I

27 Breadth-First Traversal Algorithm
In this method, After visiting a vertex v, we must visit all its adjacent vertices w1, w2, w3, ..., before going down next level to visit vertices adjacent to w1 etc. The method can be implemented using a queue. A boolean array is used to ensure that a vertex is enqueued only once. BreadthFirst(G, v) 1 enqueue the starting vertex v; mark it as visited; 2 while(queue is not empty){ dequeue a vertex v from the queue; visit v. enqueue vertices adjacent to v that are not marked visited; } Note: Adjacent vertices can be enqueued in any order; but to obtain a unique traversal, we will enqueue them in alphabetical order.

28 Example Demonstrating breadth-first traversal using a queue.
Queue front The Breadth-firstTraversal Tree is shown below: A B C D E F Order of Traversal G H I A B D E C G F H I Queue rear

29 Review Questions 1. Considera depth-first traversal of the undirected graph GA shown above, starting from vertex a. List the order in which the nodes are visited in a preorder traversal showing the depth-first traversal tree. List the order in which the nodes are visited in a postorder traversal 2. Repeat exercise 1 above for a depth-first traversal starting from vertex d. 3. List the order in which the nodes of the undirected graph GA shown above are visited by a breadth first traversal that starts from vertex a, showing the breadth-first traversal tree. Repeat this exercise for a breadth-first traversal starting from vertex d. 4. Repeat Exercises 1 and 3 for the directed graph GB.

30 Topological Sort Introduction. Definition of Topological Sort.
Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation. Review Questions.

31 Introduction There are many problems involving a set of tasks in which some of the tasks must be done before others. For example, consider the problem of taking a course only after taking its prerequisites. Is there any systematic way of linearly arranging the courses in the order that they should be taken? Yes! - Topological sort.

32 Definition of Topological Sort
Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appear in the sequence before its predecessor. The graph in (a) can be topologically sorted as in (b) (b) (a)

33 Topological Sort is not unique
The following are all topological sort of the graph below: s1 = {a, b, c, d, e, f, g, h, i} s2 = {a, c, b, f, e, d, h, g, i} s3 = {a, b, d, c, e, g, f, h, i} s4 = {a, c, f, b, e, h, d, g, i} etc.

34 Topological Sort Algorithm
One way to find a topological sort is to consider in-degrees of the vertices. The first vertex must have in-degree zero -- every DAG must have at least one vertex with in-degree zero. The Topological sort algorithm is: int topologicalOrderTraversal( ){ int numVisitedVertices = 0; while(there are more vertices to be visited){ if(there is no vertex with in-degree 0) break; else{ select a vertex v that has in-degree 0; visit v; numVisitedVertices++; delete v and all its emanating edges; } return numVisitedVertices;

35 Topological Sort Example
Demonstrating Topological Sort. A F B G C H D I E J 1 2 3 D G A B F H J E I C

36 Review Questions 1. List the order in which the nodes of the directed graph GB are visited by topological order traversal that starts from vertex a. 2. What kind of DAG has a unique topological sort? 3. Generate a directed graph using the required courses for your major. Now apply topological sort on the directed graph you obtained.


Download ppt "Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals"

Similar presentations


Ads by Google