CSC 380: Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Homework page 102 questions 1, 4, and 10 page 106 questions 4 and 5 page 111 question 1 page 119 question 9.
GRAPH Learning Outcomes Students should be able to:
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Representing and Using Graphs
Data Structures Week 9 Introduction to Graphs Consider the following problem. A river with an island and bridges. The problem is to see if there is a way.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 3 Brute Force. A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples:
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Chapter 9: Graphs.
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Graphs.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Data Structures Graphs - Terminology
Introduction to Graphs
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to Graphs
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples – based directly.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 String Matching.
CMSC 341 Lecture 21 Graphs (Introduction)
Discrete Maths 9. Graphs Objective
Refresh and Get Ready for More
Graphs Chapter 13.
Graphs CSE 2011 Winter November 2018.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CS223 Advanced Data Structures and Algorithms
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Graphs.
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
3. Brute Force Selection sort Brute-Force string matching
ITEC 2620M Introduction to Data Structures
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSC 380: Design and Analysis of Algorithms
3. Brute Force Selection sort Brute-Force string matching
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Graphs G = (V, E) V are the vertices; E are the edges.
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Introduction to Graphs
Introduction to Graphs
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

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 1i<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!