(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 12.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 158, 7] NotebookDataLength[ 20298, 527] NotebookOptionsPosition[ 19871, 509] NotebookOutlinePosition[ 20279, 525] CellTagsIndexPosition[ 20236, 522] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell[TextData[StyleBox["Agglomerative Nesting ", FontColor->RGBColor[1, 0, 0]]], "Subsubsection", CellChangeTimes->{{3.73112424028771*^9, 3.7311242546173754`*^9}, { 3.8106047392458076`*^9, 3.8106047456549196`*^9}, {3.820818867452566*^9, 3.820818868566964*^9}, {3.82082069093102*^9, 3.8208206918250113`*^9}, 3.8222000345112724`*^9, {3.8222001733534513`*^9, 3.8222001953026805`*^9}, { 3.822200422743824*^9, 3.822200423918998*^9}, 3.8222012682618866`*^9}, ExpressionUUID -> "e66d6e93-d6b2-4c5b-9154-8959b5b959a5"], Cell[BoxData[{ RowBox[{ RowBox[{ StyleBox["Input", FontColor->RGBColor[1, 0, 0]], StyleBox[":", FontColor->RGBColor[1, 0, 0]], " ", RowBox[{ RowBox[{"data", " ", "set", " ", "\[ScriptCapitalA]"}], " ", "\[Subset]", " ", SuperscriptBox["\[DoubleStruckCapitalR]", "n"]}]}], ",", " ", RowBox[{ RowBox[{"parameter", " ", "p"}], "=", RowBox[{ RowBox[{ "1", " ", "or", " ", "2", " ", "specifying", " ", "whether", " ", "to", " ", "use", " ", SubscriptBox["\[ScriptL]", "1"]}], "-", RowBox[{"norm", " ", "or", " ", "LS", " ", "distance"}], "-", RowBox[{"like", " ", "function"}]}]}], ",", RowBox[{ RowBox[{ RowBox[{ "parameter", " ", "DD", " ", "specifying", " ", "the", " ", "distance", " ", "function", " ", "between", " ", RowBox[{"sets", ":", " ", "DD"}]}], "=", RowBox[{ RowBox[{"0", ":", " ", RowBox[{"Dc", "-", RowBox[{"distance", ".", " ", "DD"}]}]}], "=", RowBox[{"1", ":", " ", RowBox[{"Dmin1", "-", "distance"}]}]}]}], ";", " ", RowBox[{"DD", "=", RowBox[{"2", ":", " ", RowBox[{"Dmin2", "-", "distance"}]}]}], ";", " ", RowBox[{"DD", "=", RowBox[{ RowBox[{"5", ":", " ", RowBox[{ RowBox[{"Ward", ".", "If"}], " ", "DD"}]}], "=", "1"}]}]}], ",", " ", RowBox[{ RowBox[{ RowBox[{"it", " ", "should", " ", "be", " ", "p"}], "=", "1"}], ";", " ", RowBox[{ RowBox[{"If", " ", "DD"}], "=", "2"}]}], ",", " ", RowBox[{ RowBox[{ RowBox[{"it", " ", "should", " ", "be", " ", "p"}], "=", "2"}], ";"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"If", " ", "Ind"}], "\[NotEqual]", "0"}], ",", " ", RowBox[{"Print", ":", " ", "DistanceMatrix"}], ",", " ", RowBox[{"Figure", ";"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["Output", FontColor->RGBColor[1, 0, 0]], StyleBox[":", FontColor->RGBColor[1, 0, 0]], " ", RowBox[{"all", " ", "partitions", " ", RowBox[{"(", RowBox[{ RowBox[{"cluster", "-", RowBox[{"centers", " ", "of", " ", "all", " ", "partitions"}]}], ",", " ", RowBox[{"objective", " ", "function", " ", "values"}], ",", RowBox[{"and", " ", "CH", " ", "and", " ", "DB", " ", "indexes"}]}], ")"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ StyleBox["Submodules", FontColor->RGBColor[1, 0, 0]], StyleBox[":", FontColor->RGBColor[1, 0, 0]], " ", RowBox[{"d", "[", "]"}]}], ",", " ", RowBox[{"FF", "[", "]"}], ",", " ", RowBox[{"Fmin", "[", "]"}], ",", " ", RowBox[{"G", "[", "]"}], ",", " ", RowBox[{"MinEl", "[", "]"}], ",", " ", RowBox[{"C1", "[", "]"}], ",", " ", RowBox[{"DC2", "[", "]"}], ",", " ", RowBox[{"Dmin1", "[", "]"}], ",", " ", RowBox[{"Dmin2", "[", "]"}], ",", " ", RowBox[{"Indexes", "[", "]"}], ",", " ", RowBox[{"HD1", "[", "]"}], ",", " ", RowBox[{"HD2", "[", "]"}]}], " "}], "\[LineSeparator]"}], "Text", CellChangeTimes->{{3.8222001228730335`*^9, 3.8222001264974904`*^9}, { 3.8222002231101017`*^9, 3.822200367715189*^9}, {3.822201351982893*^9, 3.8222013534458165`*^9}, {3.8222014251653404`*^9, 3.822201455888179*^9}, { 3.822201548437482*^9, 3.822201575838107*^9}, {3.8222016085348268`*^9, 3.8222017036871862`*^9}, {3.8222017599592466`*^9, 3.822201892630003*^9}, { 3.822201925747697*^9, 3.822201966376397*^9}, {3.822201998330805*^9, 3.8222020356851745`*^9}, {3.822275450879519*^9, 3.822275472671112*^9}, { 3.822285979755505*^9, 3.822286062590982*^9}, {3.822286096299877*^9, 3.822286096938615*^9}, {3.8222863708451605`*^9, 3.822286373300166*^9}, { 3.8222864211855383`*^9, 3.8222864231029453`*^9}}, FontSize->12, FontColor->RGBColor[0, 0, 1], ExpressionUUID -> "f6e31508-4aa7-4793-bad4-29a883041616"], Cell[BoxData[ RowBox[{ RowBox[{"AgglNest", "[", RowBox[{"A_", ",", RowBox[{"p_:", "1"}], ",", RowBox[{"DD_:", "0"}], ",", RowBox[{"Ind_:", "1"}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"PI", ",", "cen", ",", "R", ",", "i1", ",", "i2"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"PI", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "}"}], ",", RowBox[{"{", RowBox[{"i", ",", RowBox[{"Length", "[", "A", "]"}]}], "}"}]}], "]"}]}], ";", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "PI"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"slpart", "=", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"PointSize", "[", ".05", "]"}], ",", RowBox[{"Hue", "[", RowBox[{"2", RowBox[{"j", "/", RowBox[{"Length", "[", "PI", "]"}]}]}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], " ", ",", RowBox[{"PlotRange", "\[Rule]", "AG"}], ",", RowBox[{"AspectRatio", "\[Rule]", "1"}], ",", RowBox[{"GridLines", "\[Rule]", RowBox[{"{", RowBox[{"tab", ",", "tab"}], "}"}]}], ",", RowBox[{"ImageSize", "\[Rule]", "150"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"p", "\[Equal]", "1"}], ",", RowBox[{"cen", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Median", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ",", RowBox[{"cen", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mean", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}]}], "]"}], ";", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "cen"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"slcen", "=", RowBox[{"ListPlot", "[", RowBox[{"cen", ",", RowBox[{"PlotMarkers", "\[Rule]", RowBox[{"{", RowBox[{"\"\<\[SixPointedStar]\>\"", ",", "15"}], "}"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"Show", "[", RowBox[{"slpart", ",", "slcen", ",", RowBox[{"GridLines", "\[Rule]", RowBox[{"{", RowBox[{"tab", ",", "tab"}], "}"}]}], ",", RowBox[{"PlotRange", "\[Rule]", "AG"}], ",", RowBox[{"ImageSize", "\[Rule]", "Small"}]}], "]"}], "]"}], ";", RowBox[{"Print", "[", RowBox[{"\"\<|PI|=\>\"", ",", RowBox[{"Length", "[", "PI", "]"}], ",", "\"\<; F=\>\"", ",", RowBox[{ RowBox[{"Fmin", "[", RowBox[{"A", ",", "cen", ",", "p"}], "]"}], "//", "N"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", "PI", "]"}], "\[GreaterEqual]", " ", "2"}], ",", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"p", "\[Equal]", "1"}], ",", RowBox[{"R", "=", RowBox[{"DistanceMatrix", "[", RowBox[{"cen", ",", RowBox[{"DistanceFunction", "->", "ManhattanDistance"}]}], "]"}]}], ",", RowBox[{"R", "=", RowBox[{"DistanceMatrix", "[", RowBox[{"cen", ",", RowBox[{ "DistanceFunction", "\[Rule]", "SquaredEuclideanDistance"}]}], "]"}]}]}], "]"}], ";"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Which", "[", RowBox[{ RowBox[{ RowBox[{"p", "\[Equal]", "1"}], "&&", RowBox[{"DD", "\[Equal]", "0"}]}], ",", RowBox[{"R", "=", RowBox[{"DistanceMatrix", "[", RowBox[{"cen", ",", RowBox[{"DistanceFunction", "->", "ManhattanDistance"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"p", "\[Equal]", "2"}], "&&", RowBox[{"DD", "\[Equal]", "0"}]}], ",", RowBox[{"R", "=", RowBox[{"DistanceMatrix", "[", RowBox[{ RowBox[{"cen", "//", "N"}], ",", RowBox[{ "DistanceFunction", "\[Rule]", "SquaredEuclideanDistance"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"p", "\[Equal]", "2"}], "&&", RowBox[{"DD", "\[Equal]", "5"}]}], ",", RowBox[{"R", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"Length", "[", RowBox[{"PI", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}], "*", RowBox[{"Length", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}], "*", RowBox[{ RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"cen", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"cen", "[", RowBox[{"[", "j", "]"}], "]"}]}], "]"}], "^", "2"}], "/", RowBox[{"(", RowBox[{ RowBox[{"Length", "[", RowBox[{"PI", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}], "+", RowBox[{"Length", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}]}], ")"}]}]}], "//", "N"}], ",", RowBox[{"{", RowBox[{"i", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"p", "\[Equal]", "1"}], "&&", RowBox[{"DD", "\[Equal]", "1"}]}], ",", RowBox[{"R", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Dmin1", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "i", "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}]}], "]"}], ",", RowBox[{"{", RowBox[{"i", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"p", "\[Equal]", "2"}], "&&", RowBox[{"DD", "\[Equal]", "2"}]}], ",", RowBox[{"R", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Dmin2", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "i", "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}]}], "]"}], ",", RowBox[{"{", RowBox[{"i", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"TableForm", "[", "R", "]"}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"{", RowBox[{"i1", ",", "i2"}], "}"}], "=", RowBox[{"MinEl", "[", "R", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"{", RowBox[{"i1", ",", "i2"}], "}"}], ",", "\"\< : \>\"", ",", RowBox[{"{", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "i1", "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", "i2", "]"}], "]"}]}], "}"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"drop", "=", RowBox[{"Delete", "[", RowBox[{"PI", ",", RowBox[{"{", RowBox[{ RowBox[{"{", "i1", "}"}], ",", RowBox[{"{", "i2", "}"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"PI", "=", RowBox[{"Append", "[", RowBox[{"drop", ",", RowBox[{"Join", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "i1", "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", "i2", "]"}], "]"}]}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"p", "\[Equal]", "1"}], ",", RowBox[{"cen", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Median", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ",", RowBox[{"cen", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mean", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "PI", ",", "\"\<\\nNovi centri = \>\"", ",", RowBox[{"cen", "//", "N"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\<|PI|=\>\"", ",", RowBox[{"Length", "[", "PI", "]"}], ",", "\"\<; F=\>\"", ",", RowBox[{ RowBox[{"Fmin", "[", RowBox[{"A", ",", "cen", ",", "p"}], "]"}], "//", "N"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"p", "\[Equal]", "2"}], "&&", RowBox[{ RowBox[{"Length", "[", "PI", "]"}], ">", "1"}]}], ",", RowBox[{ RowBox[{"ind", "=", RowBox[{"Indexes", "[", RowBox[{"A", ",", "PI", ",", "cen"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\<; CH = \>\"", ",", RowBox[{"ind", "[", RowBox[{"[", "1", "]"}], "]"}], ",", "\"\<; DB = \>\"", ",", RowBox[{"ind", "[", RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"slpart", "=", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"PointSize", "[", ".05", "]"}], ",", RowBox[{"Hue", "[", RowBox[{"2", RowBox[{"j", "/", RowBox[{"Length", "[", "PI", "]"}]}]}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], " ", ",", RowBox[{"PlotRange", "\[Rule]", "AG"}], ",", RowBox[{"AspectRatio", "\[Rule]", "1"}], ",", RowBox[{"GridLines", "\[Rule]", RowBox[{"{", RowBox[{"tab", ",", "tab"}], "}"}]}], ",", RowBox[{"ImageSize", "\[Rule]", "150"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"slcen", "=", RowBox[{"ListPlot", "[", RowBox[{"cen", ",", RowBox[{"PlotMarkers", "\[Rule]", RowBox[{"{", RowBox[{"\"\<\[SixPointedStar]\>\"", ",", "15"}], "}"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"Show", "[", RowBox[{"slpart", ",", "slcen", ",", RowBox[{"GridLines", "\[Rule]", RowBox[{"{", RowBox[{"tab", ",", "tab"}], "}"}]}], ",", RowBox[{"PlotRange", "\[Rule]", "AG"}], ",", RowBox[{"ImageSize", "\[Rule]", "Small"}]}], "]"}], "]"}]}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}]}], "\[IndentingNewLine]", "]"}]}]], "Input", CellChangeTimes->{{3.765983189684289*^9, 3.765983205746383*^9}, { 3.7659833034012346`*^9, 3.7659834063101225`*^9}, {3.7659834402703967`*^9, 3.765983487874107*^9}, {3.7659835306839314`*^9, 3.7659836007675595`*^9}, { 3.7659836703575497`*^9, 3.7659838007709675`*^9}, {3.7659838513088865`*^9, 3.765983966108043*^9}, {3.765984053815613*^9, 3.76598405812912*^9}, 3.7659841855794153`*^9, {3.765984229315511*^9, 3.7659842430059204`*^9}, 3.7659842958856244`*^9, {3.7659845426252728`*^9, 3.7659845593465757`*^9}, 3.8222020842350855`*^9}, ExpressionUUID -> "5066c803-4d06-49ec-b39e-cf981a4e2b25"] }, Open ]] }, WindowSize->{1536, 779}, WindowMargins->{{-8, Automatic}, {Automatic, -8}}, TaggingRules->{"SlideshowSettings" -> {"Toolbar" -> True}}, FrontEndVersion->"11.0 for Microsoft Windows (64-bit) (September 21, 2016)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[580, 22, 527, 7, 39, "Subsubsection"], Cell[1110, 31, 3864, 100, 119, "Text"], Cell[4977, 133, 14878, 373, 563, "Input"] }, Open ]] } ] *)