+ All Categories
Home > Documents > Introduction This chapter explores graphs and their applications in computer science This chapter...

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

Date post: 16-Dec-2015
Category:
Upload: patience-lang
View: 215 times
Download: 1 times
Share this document with a friend
26
Introduction Introduction This chapter explores graphs and their This chapter explores graphs and their applications in computer science applications in computer science In 1736, the following problem was In 1736, the following problem was posed: In the town of Königsberg, the posed: In the town of Königsberg, the river Pregel flows around the island river Pregel flows around the island Kneiphof and then divides into two Kneiphof and then divides into two The river has four land areas (A, B, The river has four land areas (A, B, C, D) C, D) The land areas are connected using seven The land areas are connected using seven bridges, labeled a, b, c, d, e, f, and g bridges, labeled a, b, c, d, e, f, and g
Transcript

IntroductionIntroduction

This chapter explores graphs and their This chapter explores graphs and their applications in computer scienceapplications in computer science

In 1736, the following problem was In 1736, the following problem was posed: In the town of Königsberg, the posed: In the town of Königsberg, the river Pregel flows around the island river Pregel flows around the island Kneiphof and then divides into twoKneiphof 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 The land areas are connected using seven

bridges, labeled a, b, c, d, e, f, and gbridges, labeled a, b, c, d, e, f, and g

Introduction Introduction (continued)(continued)

Starting at one land area, is it possible to Starting at one land area, is it possible to walk across all the bridges exactly once walk across all the bridges exactly once and return to the starting land area?and return to the starting land area?

In 1736, Euler represented the bridge In 1736, Euler represented the bridge problem as a graphproblem as a graph

Introduction Introduction (continued)(continued)

Over the past 200 years, graph theory has Over the past 200 years, graph theory has been applied to a variety of problemsbeen applied to a variety of problems

Graphs are used to model electrical Graphs are used to model electrical circuits, chemical compounds, highway circuits, chemical compounds, highway maps, etc.maps, etc.

Graphs are used in the analysis of electrical Graphs are used in the analysis of electrical circuits, finding the shortest route, project circuits, finding the shortest route, project planning, linguistics, genetics, social planning, linguistics, genetics, social sciencescience

Graph Definitions & Graph Definitions & NotationsNotations

Let Let XX be a set be a set If a is an element of If a is an element of XX, we write a , we write a X X

A set A set YY is called a subset of is called a subset of XX if every if every element of element of YY is also an element of is also an element of XX If If YY is a subset of is a subset of XX, we write , we write YY X X

The intersection of Set The intersection of Set AA and and BB, , written written AA BB, is the set of all elements , is the set of all elements in in AA and and BB AA BB = { = {xx | | xx AA and and xx BB}}

Graph Definitions & Graph Definitions & Notations Notations (continued)(continued)

The union of Set The union of Set AA and and BB, written , written AA BB, , is the set of all elements that are in is the set of all elements that are in AA or or BB

AA BB = { = {xx | | xx AA or or xx BB}}

For sets For sets AA and and BB, the set , the set AA x Bx B, is the set , is the set of all ordered pairs of elements of of all ordered pairs of elements of AA and and BB

A x BA x B = {(a,b) | = {(a,b) | aa A, b A, b BB}}

Definitions and Definitions and TerminologyTerminology

A graph A graph GG is a pair, is a pair, GG = ( = (V, EV, E), where ), where VV is a is a finite nonempty set, called the set of finite nonempty set, called the set of vertices of vertices of GG and and EE VV x x VV

The elements of E are the pair of elements The elements of E are the pair of elements of of V, V, andand E E is called the set of edges is called the set of edges

V(GV(G) denotes the set of vertices, and ) denotes the set of vertices, and E(GE(G) ) denotes the set of edges of a graph denotes the set of edges of a graph GG

If the elements of If the elements of E(GE(G) are ordered pairs, ) are ordered pairs, GG is called a directed graph or digraph; is called a directed graph or digraph; otherwise otherwise GG is called an undirected graph is called an undirected graph

GraphsGraphs

A graph can be shown pictoriallyA graph can be shown pictorially

The vertices are drawn as circlesThe vertices are drawn as circles

Label inside the circle represents the vertexLabel inside the circle represents the vertex

In an undirected graph, the edges are In an undirected graph, the edges are drawn using linesdrawn using lines

In a directed graph, the edges are In a directed graph, the edges are drawn using arrowsdrawn using arrows

Definitions and Definitions and Terminology Terminology (continued)(continued)

In an undirected graph, the pairs In an undirected graph, the pairs ((u,vu,v) and () and (v,uv,u) represent the same ) represent the same edgeedge

A graph A graph H H is called a subgraph of is called a subgraph of the graph the graph GG if: if:

V(HV(H) ) V(GV(G) and ) and E(HE(H) ) E(GE(G))

that is, every vertex of that is, every vertex of HH is a vertex is a vertex GG, , and every edge in and every edge in HH is an edge in is an edge in GG

Definitions and Definitions and Terminology Terminology (continued)(continued)

Simple pathSimple path: all vertices except : all vertices except possibly the first and last vertices, are possibly the first and last vertices, are distinctdistinct

A cycle in A cycle in GG is a simple path in which is a simple path in which the first and last vertices are the samethe first and last vertices are the same

GG is called connected if there is a path is called connected if there is a path from any vertex to any other vertexfrom any vertex to any other vertex

A maximal subset of connected A maximal subset of connected vertices is called a component of vertices is called a component of GG

Graph RepresentationGraph Representation

To write programs that process and To write programs that process and manipulate graphs, the graphs must manipulate graphs, the graphs must be stored in computer memorybe stored in computer memory

A graph can be represented in several A graph can be represented in several ways:ways:

Adjacency matricesAdjacency matrices

Adjacency listsAdjacency lists

Adjacency MatrixAdjacency Matrix Let Let GG be a graph with n vertices, where be a graph with n vertices, where nn

> 0> 0 Let Let V(GV(G) = {) = {vv11,v,v22,…,v,…,vnn}} The adjacency matrix The adjacency matrix AAGG is a 2-dimensional is a 2-dimensional

nn x x nn matrix such that the (i,j) matrix such that the (i,j)thth entry of entry of AAGG is 1 if there is an edge from vi to vj; is 1 if there is an edge from vi to vj; otherwise, the (i,j)otherwise, the (i,j)thth entry is zero entry is zero

Adjacency ListsAdjacency Lists

Let Let GG be a graph with n vertices, where n > be a graph with n vertices, where n > 00

Let Let V(G)V(G) = { = {vv11,v,v22,…,v,…,vnn}}

In the adjacency list representation, In the adjacency list representation, corresponding to each vertex, v, there is a corresponding to each vertex, v, there is a linked list where each node of the linked list linked list where each node of the linked list contains the vertex, u, such that (contains the vertex, u, such that (u,vu,v) ) E(G)E(G)

Adjacency Lists Adjacency Lists (continued)(continued)

With n nodes, we use an array, With n nodes, we use an array, AA, of , of size size nn, such that , such that A[i]A[i] is a pointer to is a pointer to the linked list containing the vertices the linked list containing the vertices to which to which vvii is adjacent is adjacent

Each node has two components, say Each node has two components, say vertex and linkvertex and link

The component vertex contains the index The component vertex contains the index of the vertex adjacent to vertex of the vertex adjacent to vertex ii

Operations on GraphsOperations on Graphs

Operations commonly performed on a Operations commonly performed on a graph:graph: Create the graphCreate the graph Clear the graph which makes the graph emptyClear the graph which makes the graph empty Print the graphPrint the graph Determine whether the graph is emptyDetermine whether the graph is empty Traverse the graphTraverse the graph

The adjacency list (linked list) The adjacency list (linked list) representation:representation: For each vertex, v, the vertices adjacent to v For each vertex, v, the vertices adjacent to v

are stored in the linked list associated with vare stored in the linked list associated with v

Graph TraversalsGraph Traversals

Traversing a graph is similar to Traversing a graph is similar to traversing a binary tree, except thattraversing a binary tree, except that A graph might have cyclesA graph might have cycles Might not be able to traverse the entire Might not be able to traverse the entire

graph from a single vertexgraph from a single vertex

The two most common graph traversal The two most common graph traversal algorithms:algorithms: Breadth-first traversalBreadth-first traversal Depth-first traversalDepth-first traversal

Breadth-First TraversalBreadth-First Traversal

Breadth-first traversal of a graph is similar Breadth-first traversal of a graph is similar to traversing a binary tree level by level to traversing a binary tree level by level

Starting at the first vertex, the graph is Starting at the first vertex, the graph is traversed as much as possibletraversed as much as possible

Then go to the next vertex that has not yet Then go to the next vertex that has not yet been visited been visited

Use a queue to implement the breadth Use a queue to implement the breadth first search algorithmfirst search algorithm

Breadth-First Traversal Breadth-First Traversal (continued)(continued)

Breadth-First algorithm:Breadth-First algorithm:

For each vertex v in the graphFor each vertex v in the graph

If v is not visitedIf v is not visited

1. Add v to the queue1. Add v to the queue

2. Mark v as visited2. Mark v as visited

Breadth-First Traversal Breadth-First Traversal (continued)(continued)

3. While the queue is not empty3. While the queue is not empty

3.1 Remove vertex u from the queue3.1 Remove vertex u from the queue

3.2 Retrieve the vertices adjacent to 3.2 Retrieve the vertices adjacent to uu

Breadth-First Traversal Breadth-First Traversal (continued)(continued)

3.3 For each vertex w that is 3.3 For each vertex w that is adjacent to uadjacent to u

If w is not visitedIf w is not visited

3.3.a Add w to the queue3.3.a Add w to the queue

3.3.b Mark w as visited3.3.b Mark w as visited

Depth-First TraversalDepth-First Traversal

Depth-first traversal at a given node, Depth-first traversal at a given node, v:v:

1. Mark node v as visited1. Mark node v as visited

2. Visit the node2. Visit the node

3. For each vertex u adjacent to v3. For each vertex u adjacent to v

if u is not visitedif u is not visited

Start the depth first traversal at uStart the depth first traversal at u

SummarySummary 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 In an undirected graph G = (V, E), the

elements of E are unordered pairselements of E are unordered pairs In a directed graph G = (V, E), the In a directed graph G = (V, E), the

elements of E are ordered pairselements of E are ordered pairs A graph H is a subgraph of G if every A graph H is a subgraph of G if every

vertex of H is a vertex of G and every vertex of H is a vertex of G and every edge in H is an edge in Gedge in H is an edge in G

Two vertices in an undirected graph are Two vertices in an undirected graph are adjacent if there is an edge between adjacent if there is an edge between themthem

Summary Summary (continued)(continued)

LoopLoop: An edge incident on a single vertex: An edge incident on a single vertex

Simple graphSimple graph: no loops and no parallel edges: no loops and no parallel edges

Simple pathSimple path: all the vertices, except possibly : all the vertices, except possibly the first and last vertices, are distinctthe first and last vertices, are distinct

CycleCycle: a simple path in which the first and : a simple path in which the first and last vertices are the samelast vertices are the same

Undirected graph is called connected if there Undirected graph is called connected if there is a path from any vertex to any other vertexis a path from any vertex to any other vertex

TTHE HE EENDND

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 (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.


Recommended