Download presentation
Presentation is loading. Please wait.
Published byCornelius Lawson Modified over 8 years ago
1
INTRO2CS Tirgul 13 – More GUI and Optimization
2
Today: GUI Packing Events Canvas OptionMenu Optimization Traveling Sales Man Questions From Previous Exams
3
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()
4
Packing packing is a simple way to put widgets in geometric order packing only assigns by top, bottom, left, right
5
Packing (cont.) use frames in order to sub-divide a screen
6
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
7
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!
8
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.)
9
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
10
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
11
Events, Binding, and Protocol Handlers http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm Packing http://effbot.org/tkinterbook/pack.htm Tkinter widgets http://effbot.org/tkinterbook/tkinter-index.htm#class-reference A couple of tutorials http://www.python-course.eu/python_tkinter.php http://zetcode.com/gui/tkinter/ Usfull Links
12
Optimization
13
A set of possible solutions U A value function The problem: Find a solution such that V(x) is maximal/minimal Optimization Problems
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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)
22
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
23
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
24
GraphNode
25
CompleteGraph
26
CompleteGraph (cont.)
27
The Recursive Function
28
Almost Done..
29
Putting it All Together
30
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
31
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)
32
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))
33
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)
34
Recursive Function Complexity O(N) 2
35
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 https://en.wikipedia.org/wiki/Branch_and_bound Still, can only solve for about 50 cities Complexity Analysis 2 3
36
This Also Works! 2 3
37
Questions From Previous Exams
38
Question From Moed A 2015
45
Question From Moed B 2015
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.