Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 21: Graphs.
Advertisements

Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Review Binary Search Trees Operations on Binary Search Tree
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
Graph Theory.
Data Structures Using C++
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 27 Graphs and Applications.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Discrete Structures Chapter 7B Graphs Nurul Amelina Nasharuddin Multimedia Department.
Introduction to Graphs
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.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 2 Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Graphs Chapter 12.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Week 11 - Monday.  What did we talk about last time?  Binomial theorem and Pascal's triangle  Conditional probability  Bayes’ theorem.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 30 Graphs and Applications.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Discrete Mathematical Structures: Theory and Applications
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
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.
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 1 Graph Categories Strong Components Example of Digraph
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
Data Structures Graphs - Terminology
Java Programming: Program Design Including Data Structures
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs Chapter 13.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Presentation transcript:

Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science In 1736, the following problem was posed: In the town of Königsberg, the river Pregel flows around the island Kneiphof and then divides into two In 1736, the following problem was posed: In the town of Königsberg, the river Pregel flows around the island Kneiphof and then divides into two The river has four land areas (A, B, C, D) The river has four land areas (A, B, C, D) The land areas are connected using seven bridges, labeled a, b, c, d, e, f, and g The land areas are connected using seven bridges, labeled a, b, c, d, e, f, and g

Introduction (continued) Starting at one land area, is it possible to walk across all the bridges exactly once and return to the starting land area? Starting at one land area, is it possible to walk across all the bridges exactly once and return to the starting land area? In 1736, Euler represented the bridge problem as a graph In 1736, Euler represented the bridge problem as a graph

Introduction (continued) Over the past 200 years, graph theory has been applied to a variety of problems Over the past 200 years, graph theory has been applied to a variety of problems Graphs are used to model electrical circuits, chemical compounds, highway maps, etc. Graphs are used to model electrical circuits, chemical compounds, highway maps, etc. Graphs are used in the analysis of electrical circuits, finding the shortest route, project planning, linguistics, genetics, social science Graphs are used in the analysis of electrical circuits, finding the shortest route, project planning, linguistics, genetics, social science

Graph Definitions & Notations Let X be a set Let X be a set If a is an element of X, we write a  X If a is an element of X, we write a  X A set Y is called a subset of X if every element of Y is also an element of X A set Y is called a subset of X if every element of Y is also an element of X If Y is a subset of X, we write Y  X If Y is a subset of X, we write Y  X The intersection of Set A and B, written A  B, is the set of all elements in A and B The intersection of Set A and B, written A  B, is the set of all elements in A and B A  B = {x | x  A and x  B} A  B = {x | x  A and x  B}

Graph Definitions & Notations (continued) The union of Set A and B, written A  B, is the set of all elements that are in A or B The union of Set A and B, written A  B, is the set of all elements that are in A or B A  B = {x | x  A or x  B} A  B = {x | x  A or x  B} For sets A and B, the set A x B, is the set of all ordered pairs of elements of A and B For sets A and B, the set A x B, is the set of all ordered pairs of elements of A and B A x B = {(a,b) | a  A, b  B} A x B = {(a,b) | a  A, b  B}

Definitions and Terminology A graph G is a pair, G = (V, E), where V is a finite nonempty set, called the set of vertices of G and E  V x V A graph G is a pair, G = (V, E), where V is a finite nonempty set, called the set of vertices of G and E  V x V The elements of E are the pair of elements of V, and E is called the set of edges The elements of E are the pair of elements of V, and E is called the set of edges V(G) denotes the set of vertices, and E(G) denotes the set of edges of a graph G V(G) denotes the set of vertices, and E(G) denotes the set of edges of a graph G If the elements of E(G) are ordered pairs, G is called a directed graph or digraph; otherwise G is called an undirected graph If the elements of E(G) are ordered pairs, G is called a directed graph or digraph; otherwise G is called an undirected graph

Graphs A graph can be shown pictorially A graph can be shown pictorially The vertices are drawn as circles The vertices are drawn as circles Label inside the circle represents the vertex Label inside the circle represents the vertex In an undirected graph, the edges are drawn using lines In an undirected graph, the edges are drawn using lines In a directed graph, the edges are drawn using arrows In a directed graph, the edges are drawn using arrows

Definitions and Terminology (continued) In an undirected graph, the pairs (u,v) and (v,u) represent the same edge In an undirected graph, the pairs (u,v) and (v,u) represent the same edge A graph H is called a subgraph of the graph G if: A graph H is called a subgraph of the graph G if: V(H)  V(G) and E(H)  E(G) V(H)  V(G) and E(H)  E(G) that is, every vertex of H is a vertex G, and every edge in H is an edge in G that is, every vertex of H is a vertex G, and every edge in H is an edge in G

Definitions and Terminology (continued) Simple path: all vertices except possibly the first and last vertices, are distinct Simple path: all vertices except possibly the first and last vertices, are distinct A cycle in G is a simple path in which the first and last vertices are the same A cycle in G is a simple path in which the first and last vertices are the same G is called connected if there is a path from any vertex to any other vertex G is called connected if there is a path from any vertex to any other vertex A maximal subset of connected vertices is called a component of G A maximal subset of connected vertices is called a component of G

Graph Representation To write programs that process and manipulate graphs, the graphs must be stored in computer memory To write programs that process and manipulate graphs, the graphs must be stored in computer memory A graph can be represented in several ways: A graph can be represented in several ways: Adjacency matrices Adjacency matrices Adjacency lists Adjacency lists

Adjacency Matrix Let G be a graph with n vertices, where n > 0 Let G be a graph with n vertices, where n > 0 Let V(G) = {v 1,v 2,…,v n } Let V(G) = {v 1,v 2,…,v n } The adjacency matrix A G is a 2-dimensional n x n matrix such that the (i,j) th entry of A G is 1 if there is an edge from vi to vj; otherwise, the (i,j) th entry is zero The adjacency matrix A G is a 2-dimensional n x n matrix such that the (i,j) th entry of A G is 1 if there is an edge from vi to vj; otherwise, the (i,j) th entry is zero

Adjacency Lists Let G be a graph with n vertices, where n > 0 Let G be a graph with n vertices, where n > 0 Let V(G) = {v 1,v 2,…,v n } Let V(G) = {v 1,v 2,…,v n } In the adjacency list representation, corresponding to each vertex, v, there is a linked list where each node of the linked list contains the vertex, u, such that (u,v)  E(G) In the adjacency list representation, corresponding to each vertex, v, there is a linked list where each node of the linked list contains the vertex, u, such that (u,v)  E(G)

Adjacency Lists (continued) With n nodes, we use an array, A, of size n, such that A[i] is a pointer to the linked list containing the vertices to which v i is adjacent With n nodes, we use an array, A, of size n, such that A[i] is a pointer to the linked list containing the vertices to which v i is adjacent Each node has two components, say vertex and link Each node has two components, say vertex and link The component vertex contains the index of the vertex adjacent to vertex i The component vertex contains the index of the vertex adjacent to vertex i

Operations on Graphs Operations commonly performed on a graph: Operations commonly performed on a graph: Create the graph Create the graph Clear the graph which makes the graph empty Clear the graph which makes the graph empty Print the graph Print the graph Determine whether the graph is empty Determine whether the graph is empty Traverse the graph Traverse the graph The adjacency list (linked list) representation: The adjacency list (linked list) representation: For each vertex, v, the vertices adjacent to v are stored in the linked list associated with v For each vertex, v, the vertices adjacent to v are stored in the linked list associated with v

Graph Traversals Traversing a graph is similar to traversing a binary tree, except that Traversing a graph is similar to traversing a binary tree, except that A graph might have cycles A graph might have cycles Might not be able to traverse the entire graph from a single vertex Might not be able to traverse the entire graph from a single vertex The two most common graph traversal algorithms: The two most common graph traversal algorithms: Breadth-first traversal Breadth-first traversal Depth-first traversal Depth-first traversal

Breadth-First Traversal Breadth-first traversal of a graph is similar to traversing a binary tree level by level Breadth-first traversal of a graph is similar to traversing a binary tree level by level Starting at the first vertex, the graph is traversed as much as possible Starting at the first vertex, the graph is traversed as much as possible Then go to the next vertex that has not yet been visited Then go to the next vertex that has not yet been visited Use a queue to implement the breadth first search algorithm Use a queue to implement the breadth first search algorithm

Breadth-First Traversal (continued) Breadth-First algorithm: Breadth-First algorithm: For each vertex v in the graph If v is not visited If v is not visited 1. Add v to the queue 2. Mark v as visited

Breadth-First Traversal (continued) 3. While the queue is not empty 3.1 Remove vertex u from the queue 3.2 Retrieve the vertices adjacent to u

Breadth-First Traversal (continued) 3.3 For each vertex w that is adjacent to u If w is not visited 3.3.a Add w to the queue 3.3.a Add w to the queue 3.3.b Mark w as visited 3.3.b Mark w as visited

Depth-First Traversal Depth-first traversal at a given node, v: Depth-first traversal at a given node, v: 1. Mark node v as visited 2. Visit the node 3. For each vertex u adjacent to v if u is not visited if u is not visited Start the depth first traversal at u Start the depth first traversal at u

Summary A graph G is a pair, G = (V, E) A graph G is a pair, G = (V, E) In an undirected graph G = (V, E), the elements of E are unordered pairs In an undirected graph G = (V, E), the elements of E are unordered pairs In a directed graph G = (V, E), the elements of E are ordered pairs In a directed graph G = (V, E), the elements of E are ordered pairs A graph H is a subgraph of G if every vertex of H is a vertex of G and every edge in H is an edge in G A graph H is a subgraph of G if every vertex of H is a vertex of G and every edge in H is an edge in G Two vertices in an undirected graph are adjacent if there is an edge between them Two vertices in an undirected graph are adjacent if there is an edge between them

Summary (continued) Loop: An edge incident on a single vertex Loop: An edge incident on a single vertex Simple graph: no loops and no parallel edges Simple graph: no loops and no parallel edges Simple path: all the vertices, except possibly the first and last vertices, are distinct Simple path: all the vertices, except possibly the first and last vertices, are distinct Cycle: a simple path in which the first and last vertices are the same Cycle: a simple path in which the first and last vertices are the same Undirected graph is called connected if there is a path from any vertex to any other vertex Undirected graph is called connected if there is a path from any vertex to any other vertex

T HE E ND Presentation copyright 2004 Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.