Traversal algorithm (1)

Source: Internet
Author: User

Traversal algorithms are mainly used in dealing with maze problems, graphs, shortest paths, and enumeration of all possible problems. Let's take a simple example to get started with depth-first and breadth-first algorithms:

1  Packagecom.rampage.algorithm.base;2 3 Importjava.util.ArrayList;4 ImportJava.util.LinkedHashSet;5 Importjava.util.List;6 ImportJava.util.Set;7 8 /**9 * The relevant search algorithm example assumes that there are 1-9, 9 numbers, now arbitrarily fetch three, asking for a total number of different values can be obtained. If possible, output all possible values. Ten  *  One  * @authorZyq A  * -  */ -  Public classSearchalgorithm { the     Private Static intTotalCount = 0;//total number of stores -     Private Staticset<string> results =NewLinkedhashset<string> ();//Store all the results -  -      Public Static voidMain (string[] args) { + Deepfirstsearch (); - Breadthfirstsearch (); +     } A  at     /** - * Breadth-First search: The core of the breadth-first search algorithm is the list. First you get all the numbers you can put in the first step, and then you put them in the list in turn. Next, each number in the list is processed, giving the second number that may appear next, - * Remove the number from the list by processing one number in the list. And so on, until the elements in the list to be processed are empty.  -      */ -     Private Static voidBreadthfirstsearch () { -TotalCount = 0; in results.clear (); -List<string> possiblelist =NewArraylist<string>(); to  +         //initialization list, first box can be put into 1-9 -          for(inti = 1; I < 10; i++) { thePossiblelist.add (i + "")); *         } $ Panax Notoginseng         //the list is not empty when the loop processing, because it is also added to delete, at this time the reverse traversal (that is, using iterator, but iterator can not delete elements, so still error. ) -          while(!Possiblelist.isempty ()) { the              for(intI=possiblelist.size ()-1; i>=0; i--) { +String element =Possiblelist.get (i); A                 //A length of 3 indicates that 3 numbers have been spared. the                 if(element.length () = = 3) { +totalcount++; - Results.add (element); $ Possiblelist.remove (i); $                     Continue; -                 } -  the                  for(intj = 1; J < 10; J + +) { -                     //if it is already included, the number can no longer be usedWuyi                     if(Element.contains (j + ""))) { the                         Continue; -}Else { Wu                         //if no corresponding number is included, the number is appended to the element and placed in the list as a new list of possible results -Possiblelist.add (element +j); About                     } $                 } -  -                 //remove elements that have already been processed - Possiblelist.remove (i); A             } +              the         } -  $SYSTEM.OUT.PRINTLN ("Total count:" +totalcount); the System.out.println (results); the     } the  the     /** - * Using Depth-first algorithm: The core idea of the depth-first algorithm is iteration. Since you want to iterate, you should first abstract the data that needs to be processed in each step. You can think of the depth-first algorithm like this: in * Suppose there are three boxes, put 1 in the box 1th, then put 2 in box 2nd, and put 3rd in box 3. This actually makes up a combination: 123 the * To get other combinations, you must first retract the 3 in box 3rd and then put in any of the remaining ones. And so on, when the case of box 3rd is all over, you need to get back the items from box 3rd and number 2nd at the same time . the * and then start trying. In turn: About      *  the      */ the     Private Static voidDeepfirstsearch () { theTotalCount = 0; + results.clear (); -         int[] book =New int[10];//define the numbers that have been used, if you have used a number I, then book[i] = 1 the         int[] result =New int[3];//stores the current array of three numbersBayiGo (book, result, 0); theSYSTEM.OUT.PRINTLN ("Total count:" +totalcount); the System.out.println (results); -     } -  the     /** the      * @param Book the * Store the numbers that have been spared the      * @paramStep - * The first few steps, which is equivalent to the above mentioned in the box of several boxes (a box corresponding to the No. 0 step) the      */ the     Private Static voidGoint[] book,int[] result,intStep) { the         if(step = 3) {94totalcount++; theResults.add (NewString (Result[0] + "" + result[1] + "" + result[2])); the             return; the         }98          for(inti = 1; I < 10; i++) { About             if(Book[i] = = 1) { -                 Continue;101             }102Book[i] = 1;//Mark I has already been used103Result[step] =i;104             //because it is depth first, you need to continue to traverse deeper theGo (book, result, step + 1);106 107             //because there are other combinations to try, so here we go .108Book[i] = 0;109Result[step] = 0; the         }111     } the}
Searchalgorithm

The output is:

1Total count:5042 [123, 124, 125, 126, 127, 128, 129, 132, 134, 135, 136, 137, 138, 139, 142, 143, 145, 146, 147, 148, 149, 152, 153, 154, 156, 157, 158, 159, 162, 163, 164, 165, 167, 168, 169, 172, 173, 174, 175, 176, 178, 179, 182, 183, 184, 185, 186, 187, 18  9, 192, 193, 194, 195, 196, 197, 198, 213, 214, 215, 216, 217, 218, 219, 231, 234, 235, 236, 237, 238, 239, 241, 243, 245, 246, 247, 248, 249, 251, 253, 254, 256, 257, 258, 259, 261, 263, 264, 265, 267, 268, 269, 271, 273, 274, 275, 276, 278, 2 79, 281, 283, 284, 285, 286, 287, 289, 291, 293, 294, 295, 296, 297, 298, 312, 314, 315, 316, 317, 318, 319, 321, 324, 325 , 326, 327, 328, 329, 341, 342, 345, 346, 347, 348, 349, 351, 352, 354, 356, 357, 358, 359, 361, 362, 364, 365, 367, 368, 369, 371, 372, 374, 375, 376, 378, 379, 381, 382, 384, 385, 386, 387, 389, 391, 392, 394, 395, 396, 397, 398, 412, 413, 41  5, 416, 417, 418, 419, 421, 423, 425, 426, 427, 428, 429, 431, 432, 435, 436, 437, 438, 439, 451, 452, 453, 456, 457, 458, 459, 461, 462, 463, 465, 467, 468, 469, 471, 472, 473, 475, 476, 478, 479, 481, 482, 483, 485, 486, 487, 489, 491, 492, 493, 495, 496, 497, 498, 512, 513, 514, 516, 517, 518, 519, 521, 523, 524, 526, 527, 528, 529, 531, 532, 534, 536, 537, 538, 539, 541, 542, 543, 54  6, 547, 548, 549, 561, 562, 563, 564, 567, 568, 569, 571, 572, 573, 574, 576, 578, 579, 581, 582, 583, 584, 586, 587, 589, 591, 592, 593, 594, 596, 597, 598, 612, 613, 614, 615, 617, 618, 619, 621, 623, 624, 625, 627, 628, 629, 631, 632, 634, 6 35, 637, 638, 639, 641, 642, 643, 645, 647, 648, 649, 651, 652, 653, 654, 657, 658, 659, 671, 672, 673, 674, 675, 678, 679 , 681, 682, 683, 684, 685, 687, 689, 691, 692, 693, 694, 695, 697, 698, 712, 713, 714, 715, 716, 718, 719, 721, 723, 724, 725, 726, 728, 729, 731, 732, 734, 735, 736, 738, 739, 741, 742, 743, 745, 746, 748, 749, 751, 752, 753, 754, 756, 758, 75  9, 761, 762, 763, 764, 765, 768, 769, 781, 782, 783, 784, 785, 786, 789, 791, 792, 793, 794, 795, 796, 798, 812, 813, 814, 815, 816, 817, 819, 821, 823, 824, 825, 826, 827, 829, 831, 832, 834, 835, 836, 837, 839, 841, 842, 843, 845, 846, 847, 849, 851, 852, 853, 854, 856, 857, 859, 861, 862, 863, 864, 865, 867, 869, 871, 872, 873, 874, 875, 876, 879, 891, 892, 893, 894, 895, 896, 897, 91  2, 913, 914, 915, 916, 917, 918, 921, 923, 924, 925, 926, 927, 928, 931, 932, 934, 935, 936, 937, 938, 941, 942, 943, 945, 946, 947, 948, 951, 952, 953, 954, 956, 957, 958, 961, 962, 963, 964, 965, 967, 968, 971, 972, 973, 974, 975, 976, 978, 9 81, 982, 983, 984, 985, 986, 987]3Total count:5044 [918, 917, 916, 915, 914, 913, 912, 928, 927, 926, 925, 924, 923, 921, 938, 937, 936, 935, 934, 932, 931, 948, 947, 946, 945, 943, 942, 941, 958, 957, 956, 954, 953, 952, 951, 968, 967, 965, 964, 963, 962, 961, 978, 976, 975, 974, 973, 972, 97  1, 987, 986, 985, 984, 983, 982, 981, 819, 817, 816, 815, 814, 813, 812, 829, 827, 826, 825, 824, 823, 821, 839, 837, 836, 835, 834, 832, 831, 849, 847, 846, 845, 843, 842, 841, 859, 857, 856, 854, 853, 852, 851, 869, 867, 865, 864, 863, 862, 8 61, 879, 876, 875, 874, 873, 872, 871, 897, 896, 895, 894, 893, 892, 891, 719, 718, 716, 715, 714, 713, 712, 729, 728, 726 , 725, 724, 723, 721, 739, 738, 736, 735, 734, 732, 731, 749, 748, 746, 745, 743, 742, 741, 759, 758, 756, 754, 753, 752, 751, 769, 768, 765, 764, 763, 762, 761, 789, 786, 785, 784, 783, 782, 781, 798, 796, 795, 794, 793, 792, 791, 619, 618, 61  7, 615, 614, 613, 612, 629, 628, 627, 625, 624, 623, 621, 639, 638, 637, 635, 634, 632, 631, 649, 648, 647, 645, 643, 642, 641, 659, 658, 657, 654, 653, 652, 651, 679, 678, 675, 674, 673, 672, 671, 689, 687, 685, 684, 683, 682, 681, 698, 697, 695, 694, 693, 692, 691, 519, 518, 517, 516, 514, 513, 512, 529, 528, 527, 526, 524, 523, 521, 539, 538, 537, 536, 534, 532, 531, 549, 548, 547, 54  6, 543, 542, 541, 569, 568, 567, 564, 563, 562, 561, 579, 578, 576, 574, 573, 572, 571, 589, 587, 586, 584, 583, 582, 581, 598, 597, 596, 594, 593, 592, 591, 419, 418, 417, 416, 415, 413, 412, 429, 428, 427, 426, 425, 423, 421, 439, 438, 437, 4 36, 435, 432, 431, 459, 458, 457, 456, 453, 452, 451, 469, 468, 467, 465, 463, 462, 461, 479, 478, 476, 475, 473, 472, 471 , 489, 487, 486, 485, 483, 482, 481, 498, 497, 496, 495, 493, 492, 491, 319, 318, 317, 316, 315, 314, 312, 329, 328, 327, 326, 325, 324, 321, 349, 348, 347, 346, 345, 342, 341, 359, 358, 357, 356, 354, 352, 351, 369, 368, 367, 365, 364, 362, 36  1, 379, 378, 376, 375, 374, 372, 371, 389, 387, 386, 385, 384, 382, 381, 398, 397, 396, 395, 394, 392, 391, 219, 218, 217, 216, 215, 214, 213, 239, 238, 237, 236, 235, 234, 231, 249, 248, 247, 246, 245, 243, 241, 259, 258, 257, 256, 254, 253, 251, 269, 268, 267, 265, 264, 263, 261, 279, 278, 276, 275, 274, 273, 271, 289, 287, 286, 285, 284, 283, 281, 298, 297, 296, 295, 294, 293, 291, 12  9, 128, 127, 126, 125, 124, 123, 139, 138, 137, 136, 135, 134, 132, 149, 148, 147, 146, 145, 143, 142, 159, 158, 157, 156, 154, 153, 152, 169, 168, 167, 165, 164, 163, 162, 179, 178, 176, 175, 174, 173, 172, 189, 187, 186, 185, 184, 183, 182, 1 98, 197, 196, 195, 194, 193, 192]
Result

Traversal algorithm (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.