graphs. graphs similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs,...

Post on 19-Jan-2016

232 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

GraphsGraphs

GraphsGraphs

• Similar to the graphs you’ve known since the 5th grade: line graphs, bar graphs, etc., but more general.

• Those mathematical graphs are a subset of the graphs we’re going to talk about.

• A graph is made up of a set of vertices (or nodes) and a set of edges (or lines) that connect the vertices. G = { V, E }

Graph VocabularyGraph VocabularyTwo vertices are adjacent

if there is an edge between them.

A path exists between two vertices is they are connected by one or moreedges.

A simple path may not pass through the same vertex more than one time.

A path that begins and ends at the same vertex is known as a cycle.

A graph is connected if there is a path between every pair of vertices, disconnected, if not.

More Graph VocabularyMore Graph VocabularyIn a complete graph, each pair of vertices has

an edge between them.In a directed graph, or digraph,

a direction is indicated, and movement can only be in the direction indicated.◦ There may be two edges between a pair of

vertices, one in each direction.

More Graph VocabularyMore Graph Vocabulary

A weighted graph has edges labeled with numeric values.

Operations of a Graph ADTOperations of a Graph ADT1. Create an empty graph.2. Determine whether a graph is empty.3. Determine the number of vertices and/or

edges in a graph.4. Does an edge exist between 2 vertices?5. Insert a vertex that has a unique key.6. Insert an edge between 2 vertices.7. Delete an edge between 2 vertices.8. Delete a vertex and all edges to it.9. Retrieve the vertex that has a given key.

Two Ways to Represent a GraphTwo Ways to Represent a Graph

As an adjacency matrix or two-dimension array.

As an adjacency list or an array of linked lists.

Adjacency MatrixAdjacency Matrix

One row and one column for each vertexCell contains a 1 if edge exists between i and j, 0

if edge doesn’t exist.

If weighted, matrix can contain weight instead of 1.

0 1 2 3

0 0 1 1 0

1 0 0 1 0

2 1 0 0 1

3 0 0 1 0

Adjacency ListAdjacency List

A set of Lists, one for each vertex.The nodes in the lists represent vertices

adjacent to each vertex.

1 2 0

2 1

0 2

3

3

2

A Weighted Directional GraphA Weighted Directional Graph

SFO

ABQ

LGA

PVD

ABQ 900 LGA 2600

LGA 1810

SFO 2600 PVD 150

LGA 150

Which To Use?Which To Use?

Decision should be based on the application.

In most cases, list will use less memory than matrix.

Some operations are more efficient with a matrix( Is there an edge from i to j?)

Others are more efficient with a list (Find all vertices adjacent to i.)

A Graph Implementation – A Graph Implementation – A Map of MapsA Map of Maps

A B 200  C 450  D 1000     B A 200  D 540     C A 450  B 300  D 100     D B 550

Graph TraversalsGraph TraversalsTraversal visits every vertex that is

connected.You can use a traversal to find out if, in fact,

the graph is connected. If you traverse and visit every vertex, the graph is connected.

The traversal algorithm must keep track of vertices visited, so it doesn’t visit a vertex more than once, or an infinite loop may result.

Two types of traversals: Depth-First and Breadth-First.

Depth-First Search Breadth-First Search

Applications: Topological SortingApplications: Topological Sorting

Topological Order – A directed graph without cycles has a natural order.

There may be several topological orders in a graph.

Topological Sorting – Arranging the vertices into a Topological Order.

Applications: Spanning TreesApplications: Spanning TreesA Spanning Tree is a sub-graph of graph G

that has all of its vertices and enough of its edges to form a tree.

G must be an undirected graph with no cycles.◦A connected, undirected graph with n vertices

must have at least n-1 edges.◦A connected, undirected graph with n vertices

and n-1 edges cannot contain a cycle.◦A connected, undirected graph with n vertices

and more than n-1 edges must contain a cycle.

Spanning TreesSpanning Trees

b

f

e

g

c

d

h

i

a

b

f

e

g

c

d

h

i

a

b

fe

g

c

d

h

i

a

Minimum Spanning TreesMinimum Spanning Trees

Find the minimum cost path to visit all vertices in a weighted undirected graph.

Minimum Spanning Trees may not be unique, but all will have the same cost.

One simple way to find an MST is to use Prim’s Algorithm.

Prim's AlgorithmPrim's Algorithm

1. Starting from a particular vertex, mark that vertex as visited, and add it to the Minimum Spanning Tree.

2. While there are unvisited vertices:Find the minimum value edge from any one

of the visited vertices to one of the unvisited vertices.Add that edge to the Minimum Spanning

Tree, and mark the terminating vertex as visited.

Applications: Minimum Spanning Applications: Minimum Spanning TreesTrees

b

f

e

g

c

d

h

i

a

2

67

4

2

58

9

13 4

Applications: Shortest PathsApplications: Shortest Paths

In a weighted, directed graph, find the "shortest" path between two vertices.

The weight or cost of a path is the sum of the weights of the edges in the path.

Dijkstra's shortest-path algorithm actually finds the shortest paths between a specified vertex and all other vertices.

Dijkstra's AlgorithmDijkstra's Algorithm

0 1 2

3 4

8

9

4

2

1

3

2

7

1

0 1 2 3 40 ∞ 8 ∞ 9 41 ∞ ∞ 1 ∞ ∞ 2 ∞ 2 ∞ 3 ∞ 3 ∞ ∞ 2 ∞ 74 ∞ ∞ 1 ∞ ∞

Applications: CircuitsApplications: Circuits

A circuit is a type of cycle that visits each vertex or each edge exactly once.

An Euler circuit is one that begins at a specified vertex, passes through each edge exactly once, and ends at the same vertex.

Only exists if every vertex has an even number of edges.

top related