{"id":1715,"date":"2021-01-24T13:45:23","date_gmt":"2021-01-24T13:45:23","guid":{"rendered":"http:\/\/192.168.31.181\/muthu\/?p=1715"},"modified":"2021-05-24T03:53:08","modified_gmt":"2021-05-24T03:53:08","slug":"computing-the-discrete-frechet-distance-using-dynamic-programming","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/computing-the-discrete-frechet-distance-using-dynamic-programming\/","title":{"rendered":"Computing the discrete Fr\u00e9chet distance using dynamic programming"},"content":{"rendered":"\n
The Fr\u00e9chet distance is usually explained using the analogy of a man walking his dog.<\/p>\n\n\n\n A man is walking a dog on a leash: the man can move on one curve, the dog on the other; both may vary their speed, but backtracking is not allowed. What is the length of the shortest leash that is sufficient for traversing both curves?<\/p>\n\n\n\n We can formally define it as: <\/p>\n\n\n\n Given two sequence of points in \\(\\mathbb{R}^d \\), p={p1, p2, p3…..pn} and q= {q1, q2, q3…. qm}, Fr\u00e9chet distance represented by \\(f(x,y) \\) is the maximum of the minimum distances between points pi<\/sub> and qi<\/sub>. To understand it more intuitively, let us look at a simple example. <\/p>\n\n\n\n In the below graph, I have two set of polylines with positions:<\/p>\n\n\n\n \\(P = {(2,1) , (3,1), (4,2), (5,1)} \\) Starting and ending points being (p1,q1) and (p4,q3) respectively.<\/p>\n\n\n\n We find all the pairs of walks between p and q given as,<\/p>\n\n\n\n \\(p1 -> (p1, q1) , (p1,q2), (p1,q3)\\) then, we find the minimum distances for each pair.<\/p>\n\n\n\n \\(min\\{p1\\} = min\\{dist(p1,q1), dist(p1,q2), dist(p1,q3)\\} = min\\{1, 1.4, 2.2\\} = 1 \\) Now, we can find the Fr\u00e9chet distance by finding the maximum of all the minimums we calculated in the last step.<\/p>\n\n\n\n \\(f(x,y) = max\\{min\\{p1\\}, min\\{p2\\}, min\\{p3\\}, min\\{p4\\}\\} = max\\{1,1,2,2\\} = 2 \\)<\/p>\n\n\n\n The implementation described by Thomas Eiter and Heikki Mannila [1]<\/sup><\/sub> uses dynamic programming to reduce the time complexity of finding the Fr\u00e9chet distance by navigating through only three possible paths.<\/p>\n\n\n\n This reduces the total number of pairs for each point in P. The implementation is as below:<\/p>\n\n\n\n The preview of our sample as a heatmap is as below: <\/p>\n\n\n\n [1] Thomas Eiter and Heikki Mannila. Computing discrete Frechet [2] Jupyter Notebook with Frechet distance implementation. Definition The Fr\u00e9chet distance is usually explained using the analogy of a man walking his dog. A man is walking a dog on a leash: the man can move on one curve, the dog on the other; both may vary their speed, but backtracking is not allowed. What is the length of the shortest leash […]<\/p>\n","protected":false},"author":1,"featured_media":1736,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,24,6],"tags":[],"class_list":["post-1715","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithms","category-artificial-intelligence","category-mathematics"],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=1715"}],"version-history":[{"count":21,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1715\/revisions"}],"predecessor-version":[{"id":1909,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1715\/revisions\/1909"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media\/1736"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/figure>\n\n\n\n
Intuition<\/h2>\n\n\n\n
<\/figure>\n\n\n\n
\\(Q = {(2,0),(3,0),(4,0)} \\)<\/p>\n\n\n\n<\/figure>\n\n\n\n
\\(p2 -> (p2, q1), (p2,q2), (p2,q3) \\)
\\(p3 -> (p3, q2), (p3,q2), (p3,q3) \\)
\\(p4 -> (p4, q2), (p4,q2), (p4,q3) \\)<\/p>\n\n\n\n
\\(min\\{p2\\} = min\\{dist(p2,q1), dist(p2,q2), dist(p2,q3)\\} = min\\{1.4, 1, 1.4\\} = 1\\)
\\(min\\{p3\\} = min\\{dist(p3,q1), dist(p3,q2), dist(p3,q3)\\} = min\\{2.8, 2.2, 2\\} = 2 \\}\\)
\\(min\\{p4\\} = min\\{dist(p3,q1), dist(p3,q2), dist(p3,q3)\\} = min\\{3.2, 2.2, 2\\} = 2\\)<\/p>\n\n\n\nImplementation<\/h2>\n\n\n\n
from scipy.spatial.distance import euclidean\nimport seaborn as sns\n\nP = [[2,1] , [3,1], [4,2], [5,1]]\nQ = [[2,0] , [3,0], [4,0]]\n\np_length = len(P)\nq_length = len(Q)\ndistance_matrix = np.ones((p_length, q_length)) * -1\n\n# fill the first value with the distance between \n# the first two points in P and Q\ndistance_matrix[0, 0] = euclidean(P[0], Q[0])\n\n# load the first column and first row with distances (memorize)\nfor i in range(1, p_length):\n distance_matrix[i, 0] = max(distance_matrix[i-1, 0], euclidean(P[i], Q[0]))\nfor j in range(1, q_length):\n distance_matrix[0, j] = max(distance_matrix[0, j-1], euclidean(P[0], Q[j]))\n\nfor i in range(1, p_length):\n for j in range(1, q_length):\n distance_matrix[i, j] = max(min(distance_matrix[i-1, j], distance_matrix[i, j-1], distance_matrix[i-1, j-1]),\n euclidean(P[i], Q[j]))\ndistance_matrix[p_length-1, q_length-1] \nsns.heatmap(distance_matrix, annot=True)<\/code><\/pre>\n\n\n\n
References:<\/h4>\n\n\n\n
distance. Technical report, 1994. http:\/\/www.kr.tuwien.ac.at\/staff\/eiter\/et-archive\/cdtr9464.pdf<\/a><\/p>\n\n\n\n
https:\/\/colab.research.google.com\/drive\/1Cht2BSmKs6vCNfQZisoxMFZ_9eYMDrIi<\/a>
<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"