Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Class Assignment-1 done by Hassan Asghar (Sp16-BCS-073)"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": null,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "childlist = {'A':'BCF', 'B':'CD', 'C':'BDF', 'F':'CE', 'D':'E'}\n",
  17. "start = 'A'\n",
  18. "distination = 'E'\n",
  19. "visited = {'A':False, 'B':False, 'C':False, 'D':False, 'E':False, 'F':False}\n",
  20. "path = []"
  21. ]
  22. },
  23. {
  24. "cell_type": "code",
  25. "execution_count": 4,
  26. "metadata": {},
  27. "outputs": [],
  28. "source": [
  29. "def traverse(s):\n",
  30. " visited[s] = True\n",
  31. " path.append(s)\n",
  32. " if s == distination:\n",
  33. " print(path)\n",
  34. " else:\n",
  35. " child = childlist[s]\n",
  36. " for i in range(len(child)):\n",
  37. " if visited[child[i]] == False:\n",
  38. " traverse(child[i])\n",
  39. " path.pop()\n",
  40. " visited[s] = False"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": 5,
  46. "metadata": {},
  47. "outputs": [
  48. {
  49. "name": "stdout",
  50. "output_type": "stream",
  51. "text": [
  52. "['A', 'B', 'C', 'D', 'E']\n",
  53. "['A', 'B', 'C', 'F', 'E']\n",
  54. "['A', 'B', 'D', 'E']\n",
  55. "['A', 'C', 'B', 'D', 'E']\n",
  56. "['A', 'C', 'D', 'E']\n",
  57. "['A', 'C', 'F', 'E']\n",
  58. "['A', 'F', 'C', 'B', 'D', 'E']\n",
  59. "['A', 'F', 'C', 'D', 'E']\n",
  60. "['A', 'F', 'E']\n"
  61. ]
  62. }
  63. ],
  64. "source": [
  65. "traverse(start)"
  66. ]
  67. },
  68. {
  69. "cell_type": "code",
  70. "execution_count": null,
  71. "metadata": {},
  72. "outputs": [],
  73. "source": []
  74. }
  75. ],
  76. "metadata": {
  77. "kernelspec": {
  78. "display_name": "Python 3",
  79. "language": "python",
  80. "name": "python3"
  81. },
  82. "language_info": {
  83. "codemirror_mode": {
  84. "name": "ipython",
  85. "version": 3
  86. },
  87. "file_extension": ".py",
  88. "mimetype": "text/x-python",
  89. "name": "python",
  90. "nbconvert_exporter": "python",
  91. "pygments_lexer": "ipython3",
  92. "version": "3.6.5"
  93. }
  94. },
  95. "nbformat": 4,
  96. "nbformat_minor": 2
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement