cisc220 fall 2009 james atlas nov 13: graphs, line intersections

23
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections

Post on 21-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

CISC220Fall 2009

James Atlas

Nov 13: Graphs, Line Intersections

Graph Representations

• How do we represent a graph?

A B

ED

C

List Structures

V = {A, B, C, D, E}

E = {{A, B}, {A, D}, {C, E}, {D, E}}• Incidence List

– E = {{A, B}, {A, D}, {C, E}, {D, E}}

• Adjacency List– L = [A={B, D}, B={A}, C={E}, D={A, E}, E={C, D}]

A B

ED

C

Matrix Structures

V = {A, B, C, D, E}

E = {{A, B}, {A, D}, {C, E}, {D, E}}• Adjacency Matrix

A B

ED

C

A B C D E

A 1 1 0 1 0

B 1 1 0 0 0

C 0 0 1 0 1

D 1 0 0 1 1

E 0 0 1 1 1

A Directed GraphV = {A, B, C, D, E}E = {(A, B), (B, A), (B, E), (D, A), (E, A), (E, D), (E, C)}

Weighted Graph

A B C D

A 0 260 148

B 260 0

C 0

D 148 0

Disconnected Graph

Equivalence of Graphs

The numbering of the vertices, and their physical arrangement are not important. The following is the same graph as the previous slide.

Path Definition

• Adjacency

• Path– A sequence of vertices in which each successive

vertex is adjacent to its predecessor

• Connected Graph– A path from every vertex to every other vertex

• Fully Connected Graph– Every vertex is adjacent to every other vertex

Example of a Path

Example of a Cycle

Relationship Between Graphs and Trees

• A tree is a special case of a graph.

• Any connected graph which does not contain cycles can be considered a tree.

• Any node can be chosen to be the root.

What kinds of things do we do with graphs?

Breadth First Search

• Starting at a source vertex

• Systematically explore the edges to “discover” every vertex reachable from s.

• Produces a “breadth-first tree”– Root of s– Contains all vertices reachable from s– Path from s to v is the shortest path

Algorithm

1. Take a start vertex, mark it identified (color it gray), and place it into a queue.

2. While the queue is not empty1. Take a vertex, u, out of the queue (Begin visiting u)2. For all vertices v, adjacent to u,

1. If v has not been identified or visited1. Mark it identified (color it gray)2. Place it into the queue3. Add edge u, v to the Breadth First Search Tree

3. We are now done visiting u (color it black)

Example

Trace of Breadth First Search

Vertex Being Visited Queue Contents after Visit Visit Sequence 0 1 3 0 1 3 2 4 6 7 0 1 3 2 4 6 7 0 1 3 2 4 6 7 8 9 0 1 3 2 4 6 7 8 9 5 0 1 3 2 4 6 7 8 9 5 0 1 3 2 4 6 7 8 9 5 0 1 3 2 4 6 7 8 9 5 0 1 3 2 4 6 7 8 9 5 0 1 3 2 4 6 7 8 9 5 empty 0 1 3 2 4 6 7 8 9 5

Breadth-First Search Tree

vector<int> parent

[0] -1

[1] 0

[2] 1

[3] 0

[4] 1

[5] 4

[6] 1

[7] 1

[8] 2

[9] 2

Maze as a Graph

Breadth First Search Tree

Maze Solution

Find all intersections