1 data structures csci 132, spring 2014 lecture 38 graphs

18
1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

Upload: moses-wright

Post on 17-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

1

Data Structures

CSCI 132, Spring 2014Lecture 38Graphs

Page 2: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

2

Objects and ConnectionsMany problems are naturally formulated in terms of objects and the connections between them. For example:

•Airline Routes--What is the fastest (or cheapest) way to get from one city to another?•Electrical circuits--Circuit elements are wired together. How does the current flow?•Job Scheduling--The objects are tasks that need to be performed. The connections indicate which jobs should be done before which other jobs.•Links between web pages

A graph is a mathematical object that describes such situations.Algorithms for graphs are fundamental to CS.Graph Theory is a major branch of combinatorial mathematics.

Page 3: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

3

GraphsA graph is a collection of vertices, V, and edges, E.

An edge connects two vertices.

a

d

cb

a d

cb

is the same as:

Vertices: a, b, c, dEdges: ab, bc, ac, ad

Page 4: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

4

DefinitionsA path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph.

a d

cb

Example:A path from a to c could be ac or abc.

A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected.

a d

cb e

NotConnected

Page 5: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

5

More definitions

A simple path is a path with no vertex repeated.

A simple cycle is a simple path except the first and last vertex is repeated and, for an undirected graph, number of vertices >= 3.

a d

cb

Example:abca is a cycle

A tree is a graph with no cycles.

a d

cb

Page 6: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

6

Definitions ContinuedA complete graph is a graph in which all edges are present.

A sparse graph is a graph with relatively few edges.

A dense graph is a graph with many edges.

a d

cb

Complete

a d

cb

Dense

a d

cb

Sparse

Page 7: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

7

Types of Graphs

An undirected graph has no specific direction between the vertices.

A directed graph has edges that are "one way". We can go from one vertex to another, but not vice versa.

A weighted graph has weights associated with each edge. The weights can represent distances, costs, etc.

a d

cb

Undirected

a d

cb

Directed

a d

cb

34

221319

4

Weighted

Page 8: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

8

Representing Graphs as an Adjacency List

An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge.

1 2

45

3

List

1

2

3

4

5Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

Page 9: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

9

Representing Graphs as an Adjacency List

An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge.

1 2

45

3

2 5

1 5

2 4

2 5

1 2

4 3

3

4

List

1

2

3

4

5Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

Page 10: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

10

Representing a Graph with an Adjacency Matrix

An adjacency matrix is a matrix with a row and column for each vertex. The matrix entry is 1 if there is an edge between the row vertex and the column vertex.

1 2

45

3 1 2 3 4 51 0 1 0 0 12 1 0 1 1 13 0 1 0 1 04 0 1 1 0 15 1 1 0 1 0

The diagonal may be zero or one.Each edge is represented twice.

Page 11: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

11

Representing Directed GraphsIn directed graphs we only include in the list those vertices that are pointed to by a given vertex.

1 2

54

3List

1

2

3

4

5

6

6

1 2 3 4 5 6123456

Page 12: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

12

Representing Directed GraphsIn directed graphs we only include in the list those vertices that are pointed to by a given vertex.

1 2

54

3 4 2

5

5 6

2

4

List

1

2

3

4

5

6

6 6

1 2 3 4 5 61 0 1 0 1 0 02 0 0 0 0 1 03 0 0 0 0 1 14 0 1 0 0 0 05 0 0 0 1 0 06 0 0 0 0 0 1

Page 13: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

13

Representing Weighted GraphsIn weighted graphs we include the weights of each edge.

1 2

54

3List

1

2

3

4

5

6

6

1 2 3 4 5 6123456

1

2

3

45 6 7

1

Page 14: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

14

Representing Weighted GraphsIn weighted graphs we include the weights of each edge.

1 2

54

3 4 2

5

5 6

2

4

List

1

2

3

4

5

6

6 6

1 2 3 4 5 61 0 2 0 1 0 02 0 0 0 0 5 03 0 0 0 0 6 74 0 3 0 0 0 05 0 0 0 4 0 06 0 0 0 0 0 1

1

2

3

45 6 7

1

1

5

6

3

4

1

2

7

Page 15: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

15

Lists vs. MatricesSpeed:An adjacency matrix provides the fastest way to determine whether or not a particular edge is present in the graph.

For an adjacency list, there is no faster way than to search the entire list for the presence of the edge.

Memory:Normally, an adjacency matrix requires more space (n2 entries).

However, if n is small and the graph is unweighted, an adjacency matrix only needs 1 bit per entry. The adjacency list requires at least 1 word per entry (for the address of the next node of the list). This may offset the advantage of fewer entries.

Page 16: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

16

Breadth First Traversal

Problem:•Given a graph, G = (V, E), find all the vertices reachable from some source, s.

•Repeat for all unvisited vertices

Strategy:•Visit all vertices adjacent to s first. •Traversal expands outward level by level. (Visit all vertices a distance of 2 away from s second, then those at distance 3, etc.)•Keep track of nodes that have been visited already (Otherwise may visit a vertex twice or end up in an endless cycle).

Page 17: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

Pseudocode for Breadth First Traversaltemplate <int max_size>void Digraph <max_size>:: breadth_first(void (*visit)(vertex &)) const { Queue q; bool visited[max_size]; Vertex v, w, x; for (all v in G) visited[v] = false; for (all v in G) if (!visited[v]) { q.append(v); while(!q.empty( )) { q.retrieve(w); if(!visited[w]) { visited[w] = true; (*visit)(w); for (all x adjacent to w) q.append(x); } q.serve( ); } }}

Page 18: 1 Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

18

Examplea b c d

hgfe

We will work through this in class.