CSC 380: Design and Analysis of Algorithms Dr. Curry Guinn
Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu www.uncw.edu/people/guinnc 962-7937 Office Hours: MWF: 10:00am-11:00m and by appointment
Today Graphs Traveling Salesman Problem Homework 3 due this Sunday night!
Brute-Force Strengths and Weaknesses wide applicability simplicity yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting, searching, string matching) Weaknesses rarely yields efficient algorithms some brute-force algorithms are unacceptably slow A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Motivation ... Graphs are everywhere
Definitions A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E. Each edge (arc) is a pair (v, w), where v,w V. An edge may have a weight (cost). If the pair is ordered, then the graph is directed – (sometimes called a digraph). Vertex w is adjacent to v if and only if (v, w) E.
More definitions Graph: a data structure containing A set of vertices V A set of edges E, where an edge represents a connection between 2 vertices The graph at right: V = {a, b, c} E = {(a, b), (b, c), (c, a)} Assuming that a graph can only have one edge between a pair of vertices, what is the maximum number of edges a graph can contain, relative to the size of the vertex set V?
More terminology Degree: Number of edges touching a vertex Example: W has degree 4 What is the degree of X? of Z? Adjacent vertices: connected directly by an edge X U V W Z Y a c b e d f g h i j
Yet More Definitions A path is a sequence of vertices w1, w2, w3, ..., wN, such that (wi, wi+1) E for 1i<N. The length of a path is the number of edges on the path, which is N-1. A simple path is one such that all vertices are distinct, except that the first and the last could be the same. A cycle in a directed graph is a path of length at least 1 such that w1 = wN.
Definition: Connected A directed graph is acyclic if it has no cycles (Directed Acyclic Graph, abbreviated as DAG). The indegree of a vertex v is the number of edges (u, v). An undirected graph is connected if there is a path from every vertex to every other vertex. A directed graph with this property is called strongly connected.
Connected If a directed graph is not strongly connected, but the underlying graph (without direction to the arcs) is connected, then the graph is said to be weakly connected. A complete graph is a graph in which there is an edge between every pair of vertices.
A Directed Graph
Data Structures to Represent Graphs Matrix representation 2-dimensional array, A, adjacency matrix. For each edge (u, v), set A [u] [v] = 1; otherwise the entry is 0. If the edge has a weight associated with it, set A [u] [v] to the weight. Space requirement is (|V|2); alright if the graph is dense, i.e., |E| = (|V|2).
Adjacency matrix example The graph at right has the following adjacency matrix: How do we figure out the degree of a given vertex? How do we find out whether an edge exists from A to B? 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7
Pros/cons of Adj. matrix Advantage: fast to tell whether edge exists between any two vertices i and j (and to get its weight) Disadvantage: consumes a lot of memory on sparse graphs (ones with few edges)
Adjacency List Adjacency list representation For each vertex, keep a list of all adjacent vertices. For sparse graphs. Space requirement is (|E|+|V|).
Adjacency list example The graph at right has the following adjacency list: How do we figure out the degree of a given vertex? How do we find out whether an edge exists from A to B? 1 2 3 4 5 6 7 1 2 3 4 5 6 7
Pros/cons of adjacency list Advantage: New nodes can be added to the graph easily, and they can be connected with existing nodes simply by adding elements to the appropriate arrays Disadvantage: Determining whether an edge exists between two nodes requires O(n) time, where n is the average number of incident edges per node
Exhaustive Search A brute force solution to a problem involving search for an element with a special property, usually among combinatorial objects such as permutations, combinations, or subsets of a set. Method: generate a list of all potential solutions to the problem in a systematic manner (see algorithms in Sec. 5.4) evaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so far when search ends, announce the solution(s) found A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Travelling Salesman Problem (TSP) Given n cities and the costs of travelling from one city to another. Find the shortest tour that visits all cities exactly once and then returns to the starting city.
Travelling Salesman Problem (TSP) Given a weighted undirected complete graph with n nodes. Starting at node s find a cycle that visits each node exactly once and ends in s (Hamiltonian cycle) with the least weight.
Example B 3 A 8 2 2 2 C 4 E
Example B 3 A 8 2 2 2 C 4 E
The number of tours for 25 cities: Too Many Tours There is a problem with the exhaustive search strategy the number of possible tours of a map with n cities is (n − 1)! / 2 The number of tours grows incredibly quickly as we add cities to the map The number of tours for 25 cities: #cities #tours 5 12 6 60 7 360 8 2,520 9 20,160 10 181,440 310,224,200,866,619,719,680,000
Real-Life Applications The solution of several important “real world” problems is the same as finding a tour of a large number of cities transportation: school bus routes, service calls, delivering meals, ... manufacturing: an industrial robot that drills holes in printed circuit boards VLSI (microchip) layout communication: planning new telecommunication networks For many of these problems n (the number of “cities”) can be 1,000 or more
The Traveling Salesman The TSP is a famous problem first posed by Irish mathematician W. R. Hamilton in the 19th century intensely studied in operations research and other areas since 1930 This tour of 13,500 US cities was generated by an advanced algorithm that used several “tricks” to limit the number of possible tours http://www.tsp.gatech.edu/ Required 5 “CPU-years”
TSP Some visualizations: http://www.math.uwaterloo.ca/tsp/index.html http://www.math.uwaterloo.ca/tsp/concorde/downloads/downloads .htm
For Next Class, Monday Homework 3 due this Sunday night!