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.

Slides:



Advertisements
Similar presentations
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Advertisements

Review Binary Search Trees Operations on Binary Search Tree
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
PSU CS Algorithms Analysis and Design Graphs.
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.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Graphs CS3240, L. grewe.
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.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Theoretic Concepts. What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E)  V: set of vertices,
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
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.
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++
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Chapter 9: Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Lecture 9: Graphs & Graph Models. Definition of a Graph edge vertex cycle path.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Paths and circuits.
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.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Graphs Representation, BFS, DFS
Data Structures and Algorithms for Information Processing
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graph Implementation.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPHS.
Presentation transcript:

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 a graph and a tree are nonlinear data structures consisting of nodes and links between the node. Trees, however, have order within the nodes and graphs remove this requirement.

Graphs The simplest form of the graph is called an: undirected graph

Graphs We call a node a vertex and the link an edge or arc. So the following graph has vertices: V 0 - V 4 and edges: e 0 - e 5 V1V1 V0V0 V3V3 V4V4 V2V2 e0e0 e1e1 e2e2 e3e3 e4e4 e5e5

Graphs The only important information contained in this graph is the connection between nodes. How they are placed relative to each other is unimportant. V1V1 V0V0 V3V3 V4V4 V2V2 e0e0 e1e1 e2e2 e3e3 e4e4 e5e5 In other words, the appearance of the graph does not matter. Only the set of vertices and edges is necessary to define a particular graph.

Graphs An undirected graph is a finite set of vertices and connecting edges. Both sets may be empty, yielding the empty graph. Each edge is associated with two vertices and the connection itself has no order or direction. In other words, the edge that connects u to v and the the edge that connects v to u are the same edge and describe the same graph.

Undirected State Graphs Graphs are used in problem solving. If a problem can be represented as a graph, then solving the problem on the corresponding graph obtains a solution to the orginal problem. Each node represents a particular state of the problem and each edge represents a transition from one state to another. Since the rules determine what constitutes a legal move, only states that can be obtained from the current state are connected to that node.

Undirected State Graphs Winning Position Starting Position You may flip the middle shape whenever you want. You may flip either end if the other two shapes are the same. Solve this problem

Undirected State Graphs Winning Position Starting Position

Undirected State Graphs Winning Position Starting Position

Undirected State Graphs Winning Position Starting Position The graph represent the possible changes of state from one position to another. If a path can be found, then the path represents a solution.

Directed Graphs Sometimes the graph may want to represent direction in addition to the connection. We call such a graph a Directed Graph or Digraph. In a Digraph the nodes are connected with edges that have this directional property. Nodes are now considered source nodes if and edge is leaving from them and target (or sink) nodes if the edge is entering them. Instead of a line to represent an edge, we use an arrow.

Directed Graph or Digraph

Directed State Graph A directed state graph might be used to represent a game where reversing a move was not allowed. For example, the game of tic-tac-toe might have a state graph where one node represents the current board position and a second node represents a legal next move. X X X X O O O

Graphs Loops - a loop is an edge that connects a node to itself. Path - a path is a sequence of vertices, such that adjacent pairs of nodes are connected by edges. In a digraph the connection must go from the source to the target. Circuit - a circuit is path that starts and ends at the same node. Multiple Edges - a two or more edges connecting the same pair of nodes in the same direction. Simple Graph - a graph with no loops or multiple edges.

Simple Graph DE C B A A digraph may be used to represent airline flights. Each node represents a city and each path a scheduled flight.

Simple Directed Graph DE C B A For example, there is a flight from city A to city B and a flight from city B to city C, etc.

Simple Directed Graph DE C B A There are one or more paths from city B to city E.

Simple Directed Graph DE C B A A path from city B to city E exists by following the edge from city B to city C and then the edge from city C to city E.

Simple Directed Graph DE C B A A path from city B to city E exists by following the edge from city B to city D and then the edge from city D to city E.

Representing the Graph ADT Adjacency matrix - is a square grid of True/False values that represent the edges of a graph. A graph with n nodes has n rows and n columns Linked list - a graph with n nodes can be represented with n different linked lists. List I provides the connections to vertex I. Edge Set - an array of sets where each set, I, contains the vertex numbers for all nodes node I is connected to.

The Graph ADT Representing problem states as graphs provides a powerful problem solving tool. So far though, we have only expressed our problems as Abstractions. We saw pictures of how things relate to each other. We saw this same design model in all the ADTs we studied to date. First describe the behavior of the object. (member functions) Second choose a concrete representation for the object. (data)

Adjacency Matrix DE C B A ABCDEABCDE A B C D E F T F F F 1 T F T T F 2 F F F F T 3 F F F F T 4 T F F F F

Adjacency Matrix DE C B A ABCDEABCDE A B C D E F T F F F 1 T F T T F 2 F F F F T 3 F F F F T 4 T F F F F Counting the “Trues” on a row indicated how many edges originate from that node. (out degree) Counting the “Trues” in a column indicates the number of edges entering that node. (in degree)

Linked List Representation DE C B A Counting the nodes in particular list indicated how many edges originate from that node. (out degree) There is no simple analogy to indicates the number of edges entering that node. (in degree) A B C D E B ACD E A E

Adjacency Matrix ABCDEABCDE A B C D E F T F F F 1 T F T T F 2 F F F F T 3 F F F F T 4 T F F F F In general, if space is available, the adjacency matrix is the easier to use than an edge list or an edge set. Notice that the space required is great relative to the useful information. Only the “Trues” are necessary. Since most of the elements do not contribute necessary information, we call this matrix a sparse matrix.

Weighed Graph A weighed graph is a graph where the edges contain values, representing costs. The weight of a path is the total sum of all the weights of all the edges in a path. The shortest path is the one that has the smallest weight.

Weighed Graph ABCDEABCDE A B C D E Replacing T/F with the value of the edge’s weigh (cost) transforms an adjacency matrix into a weighed adjacency matrix. DE C B A

Graph Traversals Depth-First Search - repeats the following pattern From the starting vertex, v, move along a directed edge to one of vertex v’s neighbors. From that neighbor, move along a directed edge to one of its neighbors. A depth-first search always goes as far as possible down a particular path before it backs up. Does this suggest a recursive solution?

Graph Traversals Breadth-First Search - uses a queue to keep track of which vertices might still have unprocessed neighbors. All neighbors are processed first. This is moving through the graph based on the distance from the starting node. Start at the starting vertex. Mark it processed and place it in a queue. Repeat until queue becomes empty Remove a vertex, v, from the front of the queue. For each unmarked neighbor, u of v, mark u processed and place it in a queue.

Graph Traversals Both the Depth-First Search and the Breadth-First Search process only those nodes where a path exists from the starting node. Some graphs, then, contain nodes which will never be processed by either of these two search algorithms. Both will visit the same nodes, however the order in which they are processed will differ. Let’s look at an example of each search order.

Depth-First Search DE C B A Start at node B.

Depth-First Search DE C B A Move to node A

Depth-First Search E C B A Node A has reachable neighbors, go back to B and visit node C. D

Depth-First Search E C B A Node C has a neighbor, node E, so visit it. D

Depth-First Search E C B A D Node E has no reachable neighbors, not already visited. Return to node B and visit node D.

Breadth-First Search DE C B A Start at node B.

Breadth-First Search DE C B A Visit node B’s neighbor, node A.

Breadth-First Search DE C B A Visit node B’s neighbor, node C.

Breadth-First Search DE C B A Visit node B’s neighbor, node D.

Breadth-First Search DE C B A Visit node C’s neighbor, node E.

Graph Traversals Depth-First Search - B, A, C, E, D Breadth-First Search - B, A, C, D, E

Graph - Conclusion Your text contains working code to implement several of the concepts and behaviors described in this presentation. Hopefully, you now have a good introduction to graphs and their algorithms.