Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.

Similar presentations


Presentation on theme: "1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph."— Presentation transcript:

1 1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph

2 2 Introduction to Graphs Several problems in the real-world can be formulated as objects and connections between them.e.g. What is the fastest way to get from city A to city B? What is the cheapest way to get from city A to city B? We need to represent : cities and distances and cities and costs respectively.

3 3 Northwest Airline Flight Boston Hartford Atlanta Minneapolis Austin SF Seattle Anchorage

4 4 Introduction to Graphs Graphs are so important as structures that they deserve their own field called graph theory Graph theory is a branch of combinatorial mathematics and has been studied for hundreds of years. Our goal is to study graphs from the algorithmic point of view.

5 5 Introduction to Graphs Campus map Traveling salesperson Circuit layout Project scheduling Oil flow In many cases we are faced with a problem that is defined in terms of a number of objects that have some relationship among them. We then try to answer interesting questions. Graphs are the basic mathematical formulation we use to tackle such problems.

6 6 Introduction to Graphs Are all the circuits elements in a computer connected? This can be answered by looking at the model representing the circuits (objects) and the wiring of the circuit (connections) Job scheduling. Some tasks have to be processed in some pre-defined order Graphs can be used to model the order jobs are processed

7 7 Computer Network Or Internet MCI Regional Network Composed of other networks Intel Campus Umass AT&T

8 8 Application Salesman’s Route Postman’s Route Shortest route for salesman, we call it the Traveling Salesman Problem Start

9 9 Course Prerequisites CSC 122 FreshmanSophomoreJuniorSenior CSC 242

10 10 Nomenclature of Graphs A graph is a collection of vertices (nodes) and edges (arcs) Nodes are objects that have names and other properties Edges normally don't have properties but sometimes they are assigned a value – e.g. a weight. A graph can be drawn by marking points and linking them with a line

11 11 Applications --- Communication Network Vertex = city, edge = communication link. 1 2 3 8 10 4 5 9 11 6 7 Vertex Edge

12 12 Graph Definition G = (V, E) Graph = (Vertices, Edges) V is the vertex set Vertices are also called nodes or points E is the edge set Each edge connects two vertices

13 13 Concepts of Graphs edges (weight) node or vertex

14 14 Two representations of the same graph AB CD E F A B C DE F A graph is an object that is independent of its representation (in the computer or a pictorial)

15 15 Graphs and their shape In some applications the shape is used to better express the graph. If we're representing cities as they appear in the map, keep the shape of the map Still, changing it will not interfere in the solution if you do.

16 Connected Nodes Solutions base themselves on the pairs of nodes list of pairs of nodes connected that are connected in the graph 16

17 17 Number of Edges V is the number of nodes (vertices) in a graph and E represents the number of edges The number of edges in a graph can range from 0 to V(V-1)/2. We call a sparse graph a graph in which E is much less than V(V-1)/2.

18 18 Sparse Graph For example: if N = 4 - then E = N-1= O(E) (E = 3) Where E is the # of Edges There are a very small number of edges in a sparse graph where N is the number of vertices.

19 GRAPHS Definitions We call a dense a graph in which the number of edges is close to the maximal number of edges.graph We call a complete graph a graph in which E is exactly V(V-1) / 2 19

20 20 Complete Graph Total number of edges in a complete graph: E = N(N-1)/2 if N = 4, E = 6 There is an edge between any two vertices

21 21 Definitions A path from node A to node B in a graph is a list of successive connected nodes in the graph A graph is connected if there is a path from every node to every other node in the graph A graph that is not connected is made up of several connected components AB CD E F A path from C to F

22 22 What is a Graph? A Graph G consists of a set V of vertices or nodes and a set E of edges that connect the vertices. We write G=(V,E). v1v1 v5v5 v4v4 v2v2 v3v3 e1e1 e4e4 e3e3 e2e2 G=(V,E) V={v 1,v 2,v 3,v 4,v 5 } E={e 1,e 2,e 3,e 4 } e 1 =(v 1,v 2 ) e 2 =(v 2,v 3 ) e 3 =(v 2,v 4 ) e 4 =(v 3,v 5 )

23 23 v1v1 v5v5 v4v4 v2v2 v3v3 e2e2 e1e1 G=(V,E) V={v 1,v 2,v 3,v 4,v 5 } E={e 1,e 2 } e 1 =(v 2,v 3 ) e 2 =(v 2,v 4 ) Graphs --- Examples Note that not all vertices are connected

24 24 v1v1 v4v4 v2v2 v3v3 e1e1 e5e5 e4e4 e2e2 e3e3 e6e6 A Complete Graph V(V-1)/2 Fully connected

25 25 Directed Graphs In some cases we want the edges to have directions associated with them: we call such a graph a directed graph or a digraph. G=(V,E) V={v 1,v 2,v 3,v 4 } E={e 1,e 2,e 3,e 4 } e 1 =(v 2,v 1 ) e 2 =(v 1,v 3 ) e 3 =(v 1,v 4 ) e 4 =(v 4,v 3 ) e 5 =(v 3,v 4 ) v1v1 v4v4 v2v2 v3v3 e1e1 e4e4 e2e2 e3e3 ordered pair (predecessor, successor)

26 26 Undirected vs. Directed Graph Undirected Graph – no oriented edge Directed Graph – every edge has oriented vertex

27 27 Weighted Graphs In some cases, we want to associate a weight with each edge in the graph. Such a graph is known as a weighted graph. Directed graphs(as well as undirected) can also be weighted (directed weighted graphs). G=(V,E) V={v 1,v 2,v 3,v 4 } E={e 1,e 2,e 3,e 4,e 5 } ………………….. v1v1 v4v4 v2v2 v3v3 40 63 15 50 75 55

28 28 Weighted Graph Weighted graph: a graph with numbers assigned to its edges Weight: cost, distance, travel time, hop, etc. 0 1 3 2 20 10 1 5 4

29 29 Sub-graph Sub-graph: subset of vertices and edges

30 MORE TERMS A simple path is a path in which no nodes are repeated(A C E) A cycle is a path that is simple except that the first and the last nodes are the same. (ABDA) It is a path from a node back to itself. 30

31 31 Simple Path A simple path traverses a node no more than once ABCD is a simple path - its length is 3 B C D A path

32 32 Cycle – A cycle is a path that starts and ends at the same point CBDC is a simple cycle. B C D A

33 33 More Definitions A graph with no cycles is called a tree A spanning tree of a graph is a connected subgraph that contains all the nodes of the graph but has no cycles (a tree by definition) A B C DE F A B C DE F Graph Spanning Tree Spanning Tree Cycle

34 34 More Graph Terminology A vertex v j is said to be adjacent to a different vertex v i if an edge connects v i to v j, i.e., e=(v i,v j ). The length of a path is the number of edges in it. A graph with no cycles is called an acyclic graph. A directed acyclic graph is called a DAG.

35 35 More Graph Terminology Two different vertices are connected if there is a path between them. The degree of a vertex is the number of edges connected to it. C D A The degree of C is 2 – it has 2 edges

36 36 Connected vs. Unconnected Graph Connected GraphUnconnected Graph

37 37 Directed Acyclic Graph Directed Acyclic Graph (DAG) : directed graph without cycle Examples Course Requirement Graph: DAG

38 38 Directed Acyclic Graph D C B A This is NOT a DAG(Directed Acyclic Graph) ABCD is a cycle in a directed graph

39 39 Graph Representation- Matrix There are several ways to represent a graph in a computer program. The two most common ways are using a matrix or a list: An Adjacency-matrix representation: Used when the graph is dense

40 Adjacency-matrix representation: In the representation of a graph G=(V,E), it is assumed that nodes are numbered 1,2,3,...,V. The matrix representation consists of a matrix A i,j This matrix is normally symmetric along the main diagonal 40

41 41 Adjacency Matrix We have N = 20 nodes in graph Use a Matrix A[0…N-1][0…N-1] or A[N][N] if vertex i and vertex j are adjacent in graph, A[i][ j ] = 1, otherwise A[i][ j ] = 0 if vertex i has a loop, A[ i ][ i ] = 1 if vertex i has no loop, A[ i ][ i ] = 0

42 42 Adjacency Matrix--Example 1 1 4 5 6 3 2 8 7 0 00100100 00000010 00000010 00001000 00000100 00100000 00010001 00000000 01234567 0 1 2 3 4 7 5 6 0 0 0 0 0 1 0 0 8 0000000080 Arrows mark connections

43 43 Example of Adjacency Matrix 0 1 3 2 A[i][j]0123 00110 11011 21101 30110 So, Matrix A = 0 1 1 0 1 0 1 1 1 1 0 1 0 1 1 0

44 44 Adjacency Matrix --- Example 2  8  6 8  9   9  6  0123 0 1 2 3 2 3 0 1 6 8 9 The matrix is symmetric for undirected graphs. A[2][1] =A[1][2]

45 45 Undirected vs. Directed Undirected graph adjacency matrix is symmetric A[i][j]=A[j][i] Directed graph adjacency matrix may not be symmetric A[i][j]  A[j][i]

46 46 Directed Graph Matrix Representation Not symmetric A[i][j]0123 00111 10001 20001 30000 0 1 3 2 So, Matrix A = 0 1 1 1 0 0 0 1 0 0 A[2][3]  A[3][2]

47 47 Weighted Graph – Use weights in cells A[i][j]0123 0010201 110004 220005 31450 0 1 3 2 10 1 5 4 So, Matrix A = 0 10 20 1 10 0 0 4 20 0 0 5 1 4 5 0

48 48 Graph Representation Another way to represent a graph uses an array of linked lists: Adjacency-list representation: This is preferred when the graph is sparse.

49 Adjacency-list representation:  The adjacency list for a graph with N vertices consists of N Linked Lists.  The i th linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j.  In other words, AdjList[i] is a list of all nodes adjacent to i in the graph 49

50 50 Adjacency List --- Example 1 1 4 5 6 3 2 8 7 0 0 1 2 3 4 7 5 6 8 25 6 6 4 5 37 28 AdjList[]

51 51 Adjacency List An adjacency list is as an array of linked lists the ith element of the array is a list of vertices that connect to vertex i.

52 52 Example of Adjacency List 0 1 3 2 0 1 2 3 123 3 3 vertex 0 connect to vertex 1, 2 and 3 vertex 1 connects to 3 vertex 2 connects to 3

53 53 Weighted Graph Weighted graph: extend each node with an additional field: the weight 0 1 3 2 20 10 1 5 4 0 1 2 3 1 22031 01034 02035 011425

54 54 Adjacency List -Weighted --- Example 2 2 3 0 1 6 8 9 0 1 2 3 1836 0829 19 06

55 55 Space Requirement of Representations Which one is better? Memory space: adjacency matrixO(N 2 ) adjacency listO(E) where E is # of edges Sparse graph adjacency list is better Dense graph same running time for both

56 56 Comparison Of Representations where degree is the number of connected nodes CostAdjacency MatrixAdjacency List Given two vertices u and v: Do a LookUP - find out whether u and v are adjacent O(1) Find M[u][v] degree of node O(N) Given a vertex u: enumerate all neighbors of u O(N) Go through whole row degree of node O(N) For all vertices: enumerate all neighbors of each vertex O(N 2 ) Summations of all node degrees O(E)

57 57 Which is Better? Operation 1: Is there an edge from vertex i to vertex j? Operation 2: Find all vertices adjacent to vertex i. Time: Operation 1 Edge? Operation 2 Find? O(1)Search List O(d) Traverse List O(d) Traverse row O(n) MatrixList Determine which operation is most frequent.

58 58 Which is Better? Space: O(n 2 )Matrix: List: n + O(|E|) where E is number of edges AND N IS # OF NODES O(n+|E|) = O(|V| + |E|) OR THE SUM OF THE # OF EDGES AND NODES

59 59 Graph Traversal Algorithms Two algorithms Breadth First Traversal - uses a queue Depth First Traversal - uses a stack

60 60 Breadth-first Search (BFS) The breadth-first search in graphs will make use of shadings to identify nodes that have been visited White nodes are nodes not yet visited Gray nodes have been discovered but not visited, that is, not considered in the order of the breadth-first traversal Black nodes have been visited In the end of the breadth first search, a tree is formed. This tree is called BFS tree.

61 61 Breadth-first Search (Start = A ) (Queue) AB CD E F G H AB CD E F G H

62 62 AB CDF G H

63 63 Breadth-first Search-First In - is – First out AB CD E F G H A A QUEUE Take out A and put in its children- C,D,B Put in A to start - we will use a Queue OUTPUT: A

64 64 Breadth-first Search- First in First out AB CD E F G H C C QUEUE D D B B Take out C and put in its children: E,F OUTPUT: A C

65 65 Breadth-first Search First In - is – First out AB CD E F G H D D QUEUE B B E E F F Take out D and put in its children- all have been visited. OUTPUT: A C D

66 66 Breadth-first Search AB CD E F G H B B QUEUE E E F F Take out B and put in its children:G OUTPUT: A C D B

67 67 Breadth-first Search AB CD E F G H E E QUEUE F F G G Take out E and put in its children – no unvisited children OUTPUT: A C D B E

68 68 Breadth-first Search AB CD E F G H F F QUEUE G G Take out F and put in its children OUTPUT: A C D B E F

69 69 Breadth-first Search AB CD E F G H G G QUEUE H H Take out G and put in its children OUTPUT: A C D B E F G

70 70 Breadth-first Search AB CD E F G H H H QUEUE Take out H and Queue is empty OUTPUT: A C D B E F G H

71 71 Breadth-first Search AB CD E F G H QUEUE is empty

72 DEPTH FIRST SEARCH Depth First Search deeper into the graph The other major graph traversal is Depth First Search which searches deeper into the graph. ADT used is a stack last one in is the first one out approach. Now the ADT used is a stack - with the last one in is the first one out approach. The children of the first node are put in the stack, then the last child added is removed and its children are put in. Then last of the child’s children is taken out and its children are put in. And so on…….. 72

73 73 Depth-first Search (Start = A)- Use Stack AB CD E F G H OUTPUT: A

74 74 Depth-first Search AB CD E F G H A A STACK Take out A and put in its children: C, D, B OUTPUT: A

75 75 Depth-first Search AB CD E F G H C C STACK D D B B Take out B and put in its unvisited children : G OUTPUT: A B

76 76 Depth-first Search AB CD E F G H C C STACK D D G G Take out G and put in its unvisited children :H OUTPUT: A B G

77 77 Depth-first Search AB CD E F G H C C STACK D D H H Take out H and put in its unvisited children :F OUTPUT: A B G H

78 78 Depth-first Search AB CD E F G H C C STACK D D F F Take out F and put in its children- no unvisited children OUTPUT: A B G H F

79 79 Depth-first Search A B CD E F G H C C STACK D D Take out D and put in its children – no unvisited children OUTPUT: A B G H F D

80 80 Depth-first Search AB CD E F G H C C STACK Take out C and ADD E OUTPUT: A B G H F D C

81 81 Depth-first Search AB CD E F G H E E STACK Take out E and put in its children – no unvisited children and Stack is empty OUTPUT: A B G H F D C E

82 82 Depth-first Search AB CD E F G H STACK is empty OUTPUT: A B G H F D C E

83 83 Graph Traversals --- Basic Algorithm Depth First Search = structure used for storage while (An un-VISITED vertex exists) { Initialize vertex v I ; mark it as VISITED while ( stack is not empty) { remove a vertex v j from stack visit v j add un-VISITED vertices adjacent to v j to stack; mark each as VISITED }

84 84 Recursive Depth-First Algorithm 1 4 5 6 3 2 DepthFirstSearch(v i ) { visit v j ; mark v i as VISITED for each un-VISITED vertex v j adjacent to v i DepthFirstSearch (v j ) }

85 85 Graph Traversal List out all cities that United Airlines can reach from Hartford Airport CHI LA SF NYC Hartford W. DC

86 86 Demos For Traversal Algorithm Step 1: { Hartford } find neighbors of Hartford { Hartford, NYC, CHI } CHI NYC LA SF Hartford W. DC

87 87 Demos For Traversal Algorithm Step 2: { Hartford, NYC, CHI } find neighbors of NYC, CHI NYC goes to LA, CHI goes to SF { Hartford, NYC, CHI, LA, SF } CHI NYC LA SF Hartford W. DC

88 88 Demos For Traversal Algorithm Step 3: {Hartford, NYC, CHI, LA, SF } find neighbors of LA, SF no other new neighbors CHI NYC LA SF Hartford W. DC

89 89 Demos For Traversal Algorithm Finally we get all cities that United Airlines can reach from Hartford Airport {Hartford, NYC, CHI, LA, SF } CHI NYC LA SF Hartford W. DC

90 90 Graph Traversals Traversal: visit each vertex in the graph once and only once Traversal: visit each vertex in the graph once and only once. This is an a matrix of vertices. 1 4 5 6 3 2 7 9 8

91 91 Elementary Graph Operations Graph traversals provide the basis for many elementary graph operations: Spanning trees on graphs Spanning trees on graphs Graph cycles Graph cycles Connected components of a graph Connected components of a graph


Download ppt "1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph."

Similar presentations


Ads by Google