460 likes | 613 Views
This chapter delves into the essential concepts of graphs, a pivotal mathematical construct with applications spanning computer science and various other fields. It covers definitions such as vertices, edges, adjacency, paths, cycles, and connectivity, as well as different types of graphs like directed and weighted graphs. Additionally, key operations like graph traversals using Depth-First Search (DFS) and Breadth-First Search (BFS) are outlined, alongside methods for implementing graphs through adjacency matrices and lists. The chapter concludes by discussing significant applications such as topological sorting and minimum spanning trees.
E N D
Graphs Chapter 13
Preview • Graphs are an important mathematical concept that have significant applications not only in computer science, but also in many other fields. • You can view a graph as a mathematical construct, a data structure, or an abstract data type. • This chapter provides an introduction to graphs and it presents the major operations and applications of graphs that are relevant to the computer scientist Chapter 13 -- Graphs
Terminology • Def: A graph G consists of two sets • Vertices • Edges • Def: Adjacent • Two vertices are said to be adjacent if they are joined by an edge. Chapter 13 -- Graphs
Def: Subgraph • A subgraph consists of a set of a graph’s vertices and a subset of its edges Chapter 13 -- Graphs
Def: Path • A path between two vertices is a sequence of edges that begins at one vertex and ends at another. • Simple path: does not pass through the same vertex more than once. • Def: Cycle • A cycle is a path that begins and ends at the same vertex. • Def: Connected • A graph is connected if each pair of distinct vertices has a path between them Chapter 13 -- Graphs
A graph is complete if each pair of distinct vertices has an edge between them Chapter 13 -- Graphs
Since a graph has a set of edges it cannot have duplicate edges. • However, a multigraph does allow multiple edges • A graph also does not allow self-edges (loops) Chapter 13 -- Graphs
You can label the edges of a graph • When these labels represent numeric values, the graph is called a weighted graph Chapter 13 -- Graphs
All of the previous examples were of undirected graphs, because the edges did not indicate a direction. If there is direction, then the graph is called a directed graph (or digraph) Chapter 13 -- Graphs
Graphs as ADTs • Basic Functions • Create, Destroy, Determine empty • Determine number of vertices and edges • Is there an edge between I and J? • Insert a vertex, or Insert an edge • Delete a vertex, or Delete an Edge • … Chapter 13 -- Graphs
Implementing Graphs • The two most common implementations of a graph are the Adjacency Matrix and the Adjacency List • Adjacency Matrix • Given a graph G with n vertices • Matix is n x n of Booleans • Entries – True if an edge exists between I and J (false otherwise). Chapter 13 -- Graphs
Example: a directed graph. Chapter 13 -- Graphs
Example: a weighted graph Chapter 13 -- Graphs
An Adjacency List • Given a graph G with n vertices • N linked lists • The ith list has node for vertex j iff there is an edge between i and j Chapter 13 -- Graphs
Example: a directed graph Chapter 13 -- Graphs
Example: a weighted graph Chapter 13 -- Graphs
Which implementation is better? • Let’s look at the two most common operations • Determine if there is an edge (i, j) • Find all vertices adjacent to a given vertex i. • The adjacency matrix supports the first one somewhat more efficiently than the list • The second operation is supported more efficiently by the adjacency list than the matrix. Chapter 13 -- Graphs
Now consider the space requirements Chapter 13 -- Graphs
Graph Traversals • Visit all the vertices in the graph. • There are two major ways to do this: • Depth first search • Breadth first search Chapter 13 -- Graphs
Example: the order in which the nodes of a tree are visited in a depth-first search and a breadth-first search. Chapter 13 -- Graphs
How do you implement them? • Depth-First Search • Typically done with a stack. • Breadth-First Search • Typically done with a Queue Chapter 13 -- Graphs
Example: DFS • Start with Node a • Trace the Search Chapter 13 -- Graphs
Example: DFS Chapter 13 -- Graphs
Example: BFS • Start with Node a • Trace the Search Chapter 13 -- Graphs
Example: BFS Chapter 13 -- Graphs
Applications of Graphs • Topological Sorting • Given the following graph, place the vertices in order so that the edges all point in one direction Chapter 13 -- Graphs
Multiple solutions: • Applications: Prerequisites ( 201,202,236,336,308,…) Chapter 13 -- Graphs
Spanning Trees • Given a graph G, construct a tree that has all the vertices of G Chapter 13 -- Graphs
A few observations about undirected graphs • A connected undirected graph that has n vertices must have at least n-1 edges • A connected undirected graph that has n vertices and exactly n-1 edges cannot contain a cycle. • A connected undirected graph that has n vertices and more than n-1 edges must contain at least 1 cycle. Chapter 13 -- Graphs
The DFS Spanning Tree Chapter 13 -- Graphs
The BFS spanning Tree Chapter 13 -- Graphs
Minimum Spanning Trees • Given a weighted undirected graph, compute the spanning tree with the minimum cost Chapter 13 -- Graphs
Prim’s Algorithm • Start with two sets • Vertices not visited (not part of the tree) • Vertices visited (part of the tree – initialized to some start vertex) • Find the shortest edge from the visited set to the not visited set. • Add the edge to the MST • Move the vertex on the other end from not visited to visited. • Continue until all vertices are visited. Chapter 13 -- Graphs
Trace of Prim’s Chapter 13 -- Graphs
Shortest Paths • Consider once again a map of airline routes. • The shortest path from 0 to 1 in the following graph is not the edge 0-1 (weight 8) but the path (0-4-2-1) Chapter 13 -- Graphs
The algorithm to calculate the answer is attributed to Dijkstra • Need two arrays • Weight – Weight[v] = weight of the shortest path from 0 to v • Matrix – Matrix[v][u] = weight of edge from v to u • Then compare the Weight[u] with Weight[v]+Matrix[v][u]. Put the smallest in Weight[u] • Then we need a set of vertices we have visited Chapter 13 -- Graphs
We begin with the vertex set initialized to 0 Chapter 13 -- Graphs
Circuits • Eulerian Circuits • Probably the first application of graphs occurred in the early 1700s when Euler proposed the Königsberg bridge problem. • Can you start and end at the same point and only cross each bridge once? Chapter 13 -- Graphs
Some Difficult Problems • The Traveling Salesperson Problem • Def: A Hamiltonian circuit – pass through every vertex once. • Given • a weighted graph • an origin city • Problem: • the salesperson must begin at an origin city and visit all others (exactly once) and return to the origin • and do it with the least cost. Chapter 13 -- Graphs
The three utilities problem • Given • A graph (3 houses, 3 utilities) • Problem • Connect each house with each utility so the edges do not cross • This is a planarity question (minimum crossing number = 0) for K3,3 Chapter 13 -- Graphs
The four color problem • Given • A planar graph • Problem • Can you color the vertices so no two adjacent vertices have the same color? • The question was posed more than a century ago (1852) and was only solved in the 1976 (with the help of a computer) • Trivia: Nature July 17, 1879 carried a proof of this problem (which was proved false in 1890) Chapter 13 -- Graphs
Summary • Implementations: • Adjacency Matrix and Adjacency List • Graph searching • uses stacks and Queues • Trees • Connected, undirected, acyclic graphs • Problems looked at • Shortest Paths, Eulerian circuits, Hamiltonian Circuits Chapter 13 -- Graphs