Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Class Assignment-1 done by Hassan Asghar (Sp16-BCS-073)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "childlist = {'A':'BCF', 'B':'CD', 'C':'BDF', 'F':'CE', 'D':'E'}\n",
- "start = 'A'\n",
- "distination = 'E'\n",
- "visited = {'A':False, 'B':False, 'C':False, 'D':False, 'E':False, 'F':False}\n",
- "path = []"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "def traverse(s):\n",
- " visited[s] = True\n",
- " path.append(s)\n",
- " if s == distination:\n",
- " print(path)\n",
- " else:\n",
- " child = childlist[s]\n",
- " for i in range(len(child)):\n",
- " if visited[child[i]] == False:\n",
- " traverse(child[i])\n",
- " path.pop()\n",
- " visited[s] = False"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['A', 'B', 'C', 'D', 'E']\n",
- "['A', 'B', 'C', 'F', 'E']\n",
- "['A', 'B', 'D', 'E']\n",
- "['A', 'C', 'B', 'D', 'E']\n",
- "['A', 'C', 'D', 'E']\n",
- "['A', 'C', 'F', 'E']\n",
- "['A', 'F', 'C', 'B', 'D', 'E']\n",
- "['A', 'F', 'C', 'D', 'E']\n",
- "['A', 'F', 'E']\n"
- ]
- }
- ],
- "source": [
- "traverse(start)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.5"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement