Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Graphs

2 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 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 }

3 Graph Vocabulary Two vertices are adjacent if there is an edge between them. A path exists between two vertices is they are connected by one or more edges. 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.

4 More Graph Vocabulary In 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.

5 More Graph Vocabulary A weighted graph has edges labeled with numeric values.

6 Operations of a Graph ADT 1. 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.

7 Two Ways to Represent a Graph As an adjacency matrix or two- dimension array. As an adjacency list or an array of linked lists.

8 Adjacency Matrix One row and one column for each vertex Cell 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

9 Adjacency 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

10 A Weighted Directional Graph SFO ABQ LGA PVD ABQ900 LGA2600 LGA1810 SFO2600 PVD150 LGA150

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

12 A Graph Implementation – A Map of Maps AB200 C450 D1000 BA200 D540 CA450 B300 D100 DB550

13 Graph Traversals Traversal 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.

14 Depth-First Search Breadth-First Search

15 Applications: 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.

16

17 Applications: Spanning Trees A 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.

18 Spanning Trees b f e g c d h i a

19 b f e g c d h i a b f e g c d h i a

20 Minimum 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.

21 Prim'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.

22 Applications: Minimum Spanning Trees b f e g c d h i a 2 6 7 4 2 5 8 9 1 3 4

23 Applications: 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.

24 Dijkstra's Algorithm 012 34 8 9 4 2 1 3 2 7 1 0 1 2 3 4 0 ∞ 8 ∞ 9 4 1 ∞ ∞ 2 ∞ 2 ∞ 3 ∞ 3 ∞ ∞ 2 ∞ 7 4 ∞ ∞ 1 ∞ ∞

25 Applications: 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.


Download ppt "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."

Similar presentations


Ads by Google