INTRO2CS Tirgul 13 – More GUI and Optimization. Today:  GUI  Packing  Events  Canvas  OptionMenu  Optimization  Traveling Sales Man  Questions.

Slides:



Advertisements
Similar presentations
Shortest Paths Text Discrete Mathematics and Its Applications (5 th Edition) Kenneth H. Rosen Chapter 9.6 Based on slides from Chuck Allison, Michael T.
Advertisements

Types of Algorithms.
Lecture 24 Coping with NPC and Unsolvable problems. When a problem is unsolvable, that's generally very bad news: it means there is no general algorithm.
Interactive Shortest Path An Image Segmentation Technique Jonathan-Lee Jones.
1 Chapter 15.3 Hamilton Paths and Hamilton Circuits Objectives 1.Understand the definitions of Hamilton paths & Hamilton circuits. 2.Find the number of.
S. J. Shyu Chap. 1 Introduction 1 The Design and Analysis of Algorithms Chapter 1 Introduction S. J. Shyu.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
Online Algorithms for Network Design Adam Meyerson UCLA.
Randomized Planning for Short Inspection Paths Tim Danner Lydia E. Kavraki Department of Computer Science Rice University.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Solution methods for Discrete Optimization Problems.
Approximation Algorithms Motivation and Definitions TSP Vertex Cover Scheduling.
Backtracking.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Lecture 23. Greedy Algorithms
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
University of Texas at Arlington Srikanth Vadada Kishan Kumar B P Fall CSE 5311 Solving Travelling Salesman Problem for Metric Graphs using MST.
Graph Theory Hamilton Paths and Hamilton Circuits.
Fundamentals of Algorithms MCS - 2 Lecture # 7
Representing and Using Graphs
1 Excursions in Modern Mathematics Sixth Edition Peter Tannenbaum.
6.1 Hamilton Circuits and Paths: Hamilton Circuits and Paths: Hamilton Path: Travels to each vertex once and only once… Hamilton Path: Travels to each.
© 2010 Pearson Prentice Hall. All rights reserved. 1 §15.3, Hamilton Paths and Circuits.
Spring 2015 Mathematics in Management Science Traveling Salesman Problem Approximate solutions for TSP NNA, RNN, SEA Greedy Heuristic Algorithms.
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
Copyright © 2014, 2010, 2007 Pearson Education, Inc. Section 4.2, Slide 1 4 Graph Theory (Networks) The Mathematics of Relationships 4.
DP TSP Have some numbering of the vertices We will think of all our tours as beginning and ending at 1 C(S, j) = length of shortest path visiting each.
The Traveling Salesman Problem Over Seventy Years of Research, and a Million in Cash Presented by Vladimir Coxall.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
1 Branch and Bound Searching Strategies Updated: 12/27/2010.
Introduction to Genetic Algorithms. Genetic Algorithms We’ve covered enough material that we can write programs that use genetic algorithms! –More advanced.
Traveling Salesman Problem (TSP)
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
Tkinter Basics. Initial Stuff To get the package: from tkinter import * To make a window, give it a title and operate it: windowVar = Tk() windowVar.title(“your.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
CIRCUITS, PATHS, AND SCHEDULES Euler and Königsberg.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]
I can describe the differences between Hamilton and Euler circuits and find efficient Hamilton circuits in graphs. Hamilton Circuits I can compare and.
Grade 11 AP Mathematics Graph Theory Definition: A graph, G, is a set of vertices v(G) = {v 1, v 2, v 3, …, v n } and edges e(G) = {v i v j where 1 ≤ i,
Mathematical modeling To describe or represent a real-world situation quantitatively, in mathematical language.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graphs 1 Neil Ghani University of Strathclyde. Where are we …. We studied lists: * Searching and sorting a list Then we studied trees: * Efficient search.
Excursions in Modern Mathematics Sixth Edition
Weighted Graphs and traveling Salesperson problem
Shortest Path Problems
BackTracking CS255.
Routing Through Networks - 1
Types of Algorithms.
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
Discrete Mathematics and Its Applications (5th Edition)
Types of Algorithms.
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
Chapter 2: Business Efficiency Business Efficiency
Advanced Analysis of Algorithms
Shortest Paths and Minimum Spanning Trees
3. Brute Force Selection sort Brute-Force string matching
Discrete Mathematics and Its Applications (5th Edition)
3. Brute Force Selection sort Brute-Force string matching
Backtracking and Branch-and-Bound
Types of Algorithms.
Hamilton Paths and Hamilton Circuits
Shortest Paths Discrete Mathematics and Its Applications (8th Edition)
Traveling Salesman Problems Nearest Neighbor Method
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

INTRO2CS Tirgul 13 – More GUI and Optimization

Today:  GUI  Packing  Events  Canvas  OptionMenu  Optimization  Traveling Sales Man  Questions From Previous Exams

GUI in Python - Recap  In Python, we interact with the GUI using widgets  Widgets are placed inside other widgets  The base widget is the root, initialized by Tk()  The GUI starts “running” when we call root.mainloop()

Packing  packing is a simple way to put widgets in geometric order  packing only assigns by top, bottom, left, right

Packing (cont.)  use frames in order to sub-divide a screen

 Some widgets (such as Button) can have a command associated with them  General interactions with a widget (such as mouse over, mouse clicks, etc) can have a handler function bound to them  The handler function is automatically passed an event object Events Recap

 We can also define what happens when we interact with the GUI window  This is done similarly to bind, using the protocol method  A useful protocol is WM_DELETE_WINDOW, for when the window ‘x’ button is pressed Events (cont.) We need to explicitly destroy the window!

 Another way to generate an event is to schedule it using after  Using after can also be used to generate sub- loops of mainloop! Events (cont.)

The Canvas Widget  The Canvas widget provides an area on which things can be displayed (“drawn”)  Drawing is done via different create methods  The create method returns an item id

The OptionMenu Widget  Allows a selection from multiple options  Takes a VariableClass object and options for it  A VariableClass state can be queried by it’s get() method

 Events, Binding, and Protocol Handlers  Packing  Tkinter widgets  A couple of tutorials Usfull Links

Optimization

 A set of possible solutions U  A value function  The problem: Find a solution such that V(x) is maximal/minimal Optimization Problems

 Most questions you can think of can be phrased as an optimization problem!  Every problem in which we need to find some solution can be phrased as an optimization problem  Soduko  8 Queens Optimization Problems

 Some optimization problems have an efficient algorithms to solve them  But in many cases, finding an optimal solution is computationally hard  Some times we are fine with just finding an approximated solution to the problem  More about this in the Algorithms course! Optimization Problems

 There are N different cities  The cities are connected by a network of roads  Each road takes a different time to travel  A salesman wishes to travel through all the cities  The salesman begins at a specific city and has to finish there also  The problem: finding the fastest route The Traveling Salesman Problem

 What is the set of solutions?  What is the value function?  The problem can be represented using a graph:  Each city is a vertex (|V| = N)  Each road between two cities is assigned an edge  Each edge is assigned the time it takes to travel the respective road TSP as an optimization problem

 TSP is a hard problem!  Efficient approximation algorithms exist  However, finding the shortest path between 2 nodes in a graph is possible in O(|V| ) TSP as an optimization problem 2

 We can assume our graph is complete (every two vertices are connected by an edge)  We can also assume each edge is assigned the minimal time it takes to travel between the cities it connects  How can we solve it? TSP as an optimization problem

 We could try a greedy algorithm  Move to the nearest city in each step  But as we already know – the result might not be optimal TSP Greedy Algorithm

 We could a local search:  For a given permutation P, define a neighbor permutation as a permutation where we switched the order of 2 cities in P TSP Local Search (1,2,3,4) (2,1,3,4)(3,2,1,4) (4,2,3,1) (1,3,2,4)(1,4,3,2) (1,2,4,3)

 We could a local search:  For a given permutation P, define a neighbor permutation as a permutation where we switched the order of 2 cities in P  Start with a random permutation  Each step - move to a neighbor permutation with a better value (while there is one)  Local search will result in a local optimum! TSP Local Search

 We could use brute force recursion!  Actually very similar to best known algorithm  Algorithm idea:  Recursively try each permutation of city ordering  Each branch of the recursion tree returns the best result it found TSP Simple Algorithm

GraphNode

CompleteGraph

CompleteGraph (cont.)

The Recursive Function

Almost Done..

Putting it All Together

 There are N! permutations of the order to visit the cities  For each permutation, the recursive function is called O(N) times  Other functions complexity is negligible Complexity Analysis

List Complexity Reminder OperationAverage CaseAmortized Worst Case CopyO(n) AppendO(1) InsertO(n) Get ItemO(1) Set ItemO(1) Delete ItemO(n) IterationO(n) Get SliceO(k) Del SliceO(n) Set SliceO(k+n) ExtendO(k) SortO(n log n) MultiplyO(nk) x in sO(n) min(s), max(s)O(n) Get LengthO(1)

Set Complexity Reminder OperationAverage caseWorst Case x in sO(1)O(n) Union s|tO(len(s)+len(t)) Intersection s&t O(min(len(s), len(t)) O(len(s) * len(t)) Multiple intersection s 1 &s 2 &..&s n (n-1)*O(L) L = max(len(s 1 ),..,len(s n )) Difference s-tO(len(s)) s.difference_update(t)O(len(t)) Symmetric Difference s^tO(len(s))O(len(s) * len(t)) s.symmetric_difference_update(t)O(len(t))O(len(t) * len(s))

Dict Complexity Reminder OperationAverage Case Amortized Worst Case CopyO(n) Get ItemO(1)O(n) Set ItemO(1)O(n) Delete ItemO(1)O(n) IterationO(n)

Recursive Function Complexity O(N) 2

 So the overall complexity is O(N!*N*N )=O(N *N!)  The problem is with the factorial factor  Best (optimal) algorithm avoids paths it can tell in advance are irrelevant. See  Still, can only solve for about 50 cities Complexity Analysis 2 3

This Also Works! 2 3

Questions From Previous Exams

Question From Moed A 2015

Question From Moed B 2015