80 likes | 185 Views
Warming up for the 2 nd Term. 1. An example - Solving a problem from graph theory 2. A Cryptarithmetic Puzzle Another case study. Problem: constructing a program for drawing a graph using a continuous line. Issues: Which algorithm? How to represent data? Efficient implementation.
E N D
Warming up for the 2nd Term 1. An example - Solving a problem from graph theory 2. A Cryptarithmetic Puzzle Another case study
Problem: constructing a program for drawing a graph using a continuous line. Issues: • Which algorithm? • How to represent data? • Efficient implementation
Data representation - 2 alternatives 1 use a set of facts edge(a,b). edge(a,c). edge(a,d). edge(b,c). edge(b,d). edge(c,e). edge(c,d). edge(d,e). total_number_of_edges(8). 2 use a list of edges graph([a-b, a-c, a-d, b-c, b-d, c-e, c-d, d-e]). Algorithm – use a simple blind search
Program 1 – using the 1st representation • Very similar to what we did for London tube • Adding a counter to count down how many edges left • draw1( Sol, PartialSol, Counter) draw1(Sol, Sol, 0). % base draw1(Sol, [Y-Z|ParSol], N):- N > 0, (edge(X,Y) ; edge(Y,X)), N1 is N-1, \+ member(X-Y, [Y-Z|ParSol]), \+ member(Y-X, [Y-Z|ParSol]), draw1(Sol, [X-Y,Y-Z|ParSol], N1). % top level draw1(Sol):- total_number_of_edges(N), (edge(X,Y); edge(Y,X)), N1 is N-1, draw2(Sol, [X-Y], N1).
Program 2 – using the 2nd representation • Using delete program (see next slide) • Don’t need membership checking • draw2( EdgesNeedToDraw, Sol ) draw2([X],[X]). % base draw2(G,Sol):- (delete(X-Y, G, T); delete(Y-X, G, T)), Sol = [X-Y,Y-Z|R], draw2(T, [Y-Z|R]). % top level draw2(Sol):- graph(G), draw2(G, Sol).
A useful program for search problems % delete( an_item, a_list, list_after_delete) delete( X, [X|T], T). delete( X, [Y|T], [Y|R]) :- delete( X, T, R ).
How to assign decimal digits to letters, so that the following sum is valid? Assume that we already know that The possible digits set for the letters is {0,1,2,…9}; M must be 1; Two different letters can't be assigned the same digit SEND + MORE -------- MONEY A Cryptarithmetic Puzzle Problem Statement
using c1,c2 and c3 to represent carrieschanging problem to solve 4 equations S E N D D+E = Y+10*C1 M O R E C1+N+R = E+10*C2 + C3 C2 C1 C2+E+O = N+10*C3 ---------------------- C3+S+M = O+10*M M O N E Y