260 likes | 564 Views
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.
E N D
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 } Graphs
Two vertices are adjacentif 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. 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. More Graph Vocabulary
A weighted graph has edges labeled with numeric values. More Graph Vocabulary
Create an empty graph. • Determine whether a graph is empty. • Determine the number of vertices and/or edges in a graph. • Does an edge exist between 2 vertices? • Insert a vertex that has a unique key. • Insert an edge between 2 vertices. • Delete an edge between 2 vertices. • Delete a vertex and all edges to it. • Retrieve the vertex that has a given key. Operations of a Graph ADT
As an adjacency matrix or two-dimension array. • As an adjacency list or an array of linked lists. Two Ways to Represent a Graph
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 Adjacency Matrix
A set of Lists, one for each vertex. • The nodes in the lists represent vertices adjacent to each vertex. 1 2 2 0 3 2 0 1 2 Adjacency List 3
SFO PVD ABQ SFO LGA LGA LGA 1810 150 150 2600 2600 900 ABQ A Weighted Directional Graph LGA PVD
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.) Which To Use?
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. Graph Traversals
Breadth-First Search Depth-First Search
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: Topological Sorting
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. Applications: Spanning Trees
b a c h e d i g f Spanning Trees
a b b a c i f e c h e d i g d g h f
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. Minimum Spanning Trees
Starting from a particular vertex, mark that vertex as visited, and add it to the Minimum Spanning Tree. 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. Prim's Algorithm
6 b 7 a 9 c 2 h 4 3 e 1 d 4 i 8 5 g 2 f Applications: Minimum Spanning Trees
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. Applications: Shortest Paths
8 2 0 1 2 1 4 3 2 1 9 3 4 7 0 1 2 3 4 0 ∞ 8 ∞ 9 4 1 ∞ ∞ 1 ∞ ∞ 2 ∞ 2 ∞ 3 ∞ 3 ∞ ∞ 2 ∞ 7 4 ∞ ∞ 1 ∞ ∞ Dijkstra's Algorithm
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. Applications: Circuits