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

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Introduction to Graphs
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graph & BFS.
Graphs Chapter 13.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 9: Graphs Basic Concepts
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Chapter 9: Graphs Basic Concepts
GRAPHS.
Presentation transcript:

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

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 Northwest Airline Flight Boston Hartford Atlanta Minneapolis Austin SF Seattle Anchorage

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 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 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 Computer Network Or Internet MCI Regional Network Composed of other networks Intel Campus Umass AT&T

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

9 Course Prerequisites CSC 122 FreshmanSophomoreJuniorSenior CSC 242

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 Applications --- Communication Network Vertex = city, edge = communication link Vertex Edge

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 Concepts of Graphs edges (weight) node or vertex

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 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.

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

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 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.

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 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 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 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 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 v1v1 v4v4 v2v2 v3v3 e1e1 e5e5 e4e4 e2e2 e3e3 e6e6 A Complete Graph V(V-1)/2 Fully connected

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 Undirected vs. Directed Graph Undirected Graph – no oriented edge Directed Graph – every edge has oriented vertex

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 v3v

28 Weighted Graph Weighted graph: a graph with numbers assigned to its edges Weight: cost, distance, travel time, hop, etc

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

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 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 Cycle – A cycle is a path that starts and ends at the same point CBDC is a simple cycle. B C D A

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 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 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 Connected vs. Unconnected Graph Connected GraphUnconnected Graph

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

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 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

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 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 Adjacency Matrix--Example Arrows mark connections

43 Example of Adjacency Matrix A[i][j] So, Matrix A =

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

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 Directed Graph Matrix Representation Not symmetric A[i][j] So, Matrix A = A[2][3]  A[3][2]

47 Weighted Graph – Use weights in cells A[i][j] So, Matrix A =

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.

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 Adjacency List --- Example AdjList[]

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 Example of Adjacency List vertex 0 connect to vertex 1, 2 and 3 vertex 1 connects to 3 vertex 2 connects to 3

53 Weighted Graph Weighted graph: extend each node with an additional field: the weight

54 Adjacency List -Weighted --- Example

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 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 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 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 Graph Traversal Algorithms Two algorithms Breadth First Traversal - uses a queue Depth First Traversal - uses a stack

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 Breadth-first Search (Start = A ) (Queue) AB CD E F G H AB CD E F G H

62 AB CDF G H

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 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 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 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 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 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 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 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 Breadth-first Search AB CD E F G H QUEUE is empty

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 Depth-first Search (Start = A)- Use Stack AB CD E F G H OUTPUT: A

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 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 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 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 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 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 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 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 Depth-first Search AB CD E F G H STACK is empty OUTPUT: A B G H F D C E

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 Recursive Depth-First Algorithm 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 Graph Traversal List out all cities that United Airlines can reach from Hartford Airport CHI LA SF NYC Hartford W. DC

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

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 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 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 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

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