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

25
Graphs Graphs

Transcript of Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs,...

Page 1: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

GraphsGraphs

Page 2: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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 }

Page 3: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 4: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 5: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

More Graph VocabularyMore Graph Vocabulary

A weighted graph has edges labeled with numeric values.

Page 6: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 7: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 8: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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

Page 9: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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

Page 10: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

A Weighted Directional GraphA Weighted Directional Graph

SFO

ABQ

LGA

PVD

ABQ 900 LGA 2600

LGA 1810

SFO 2600 PVD 150

LGA 150

Page 11: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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

Page 12: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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

Page 13: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 14: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

Depth-First Search Breadth-First Search

Page 15: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 16: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Page 17: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 18: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

Spanning TreesSpanning Trees

b

f

e

g

c

d

h

i

a

Page 19: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

b

f

e

g

c

d

h

i

a

b

fe

g

c

d

h

i

a

Page 20: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 21: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 22: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

Applications: Minimum Spanning Applications: Minimum Spanning TreesTrees

b

f

e

g

c

d

h

i

a

2

67

4

2

58

9

13 4

Page 23: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.

Page 24: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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 ∞ ∞

Page 25: Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.

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.