Download presentation

Presentation is loading. Please wait.

Published byPatience Lang Modified about 1 year ago

1
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

2
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

3
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

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

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

6
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

7
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

10
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

11
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

12
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

13
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

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

15
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

17
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

18
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

19
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

20
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

21
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

22
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

23
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

24
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

25
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

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google