├── .gitignore ├── .obsidian ├── app.json ├── appearance.json ├── community-plugins.json ├── core-plugins-migration.json ├── core-plugins.json ├── graph.json ├── hotkeys.json ├── plugins │ ├── dataview │ │ ├── main.js │ │ ├── manifest.json │ │ └── styles.css │ ├── obsidian-admonition │ │ ├── data.json │ │ ├── main.js │ │ ├── manifest.json │ │ └── styles.css │ ├── obsidian-git │ │ ├── data.json │ │ ├── main.js │ │ ├── manifest.json │ │ └── styles.css │ └── table-editor-obsidian │ │ ├── data.json │ │ ├── main.js │ │ ├── manifest.json │ │ └── styles.css ├── themes │ ├── Material Gruvbox │ │ ├── manifest.json │ │ └── theme.css │ └── Obsidian gruvbox │ │ ├── manifest.json │ │ └── theme.css └── workspace.json ├── Computer Networks ├── 100-Internet.md ├── 101-Network Edge.md ├── 102-Network Core.md ├── 103-Delay, Loss and Throughput.md ├── 104-Protocol Layers.md ├── 105-Networks Under Attack.md ├── 200-Application Layer.md ├── 201-Principles of Network Applications.md ├── 202-Web and HTTP.md ├── 203-Cookies.md ├── 204-Web Cache.md ├── 205-DNS.md ├── 206-DNS Records and Messages.md ├── 207-P2P File Distribution.md ├── 208-Socket Programming.md ├── 300-Introductoin and Transport-Layer Services.md ├── 301-Multiplexing and Demultiplexing.md ├── 302-UDP.md ├── 303-Principles of Reliable Data Transfer.md ├── 304-Pipelined RDT Protocols.md └── Images │ ├── apparch.png │ ├── bittorrent.png │ ├── cable.png │ ├── ciruit.png │ ├── cookie.png │ ├── ddos.png │ ├── delay.png │ ├── dns.png │ ├── dnsiter.png │ ├── dnsmsg.png │ ├── dnsrecur.png │ ├── dsl.png │ ├── ftth.png │ ├── gbn.png │ ├── gbnfsm.png │ ├── gbnop.png │ ├── isp.png │ ├── multiplexing.png │ ├── nutsbolts1.png │ ├── nutsbolts2.png │ ├── p2p.png │ ├── p2pg.png │ ├── pipelined.png │ ├── pipelined2.png │ ├── port.png │ ├── protlay.png │ ├── protocol.png │ ├── proxy.png │ ├── rdt10.png │ ├── rdt20.png │ ├── rdt21r.png │ ├── rdt21s.png │ ├── rdt22r.png │ ├── rdt22s.png │ ├── rdt30.png │ ├── rdt30w.png │ ├── reliabledata.png │ ├── socket.png │ ├── sr.png │ ├── srop.png │ ├── srwin.png │ ├── tcpsocket.png │ ├── tcpsocket2.png │ ├── tcpudp.png │ ├── tcpvudp.png │ ├── traceroute.png │ ├── trafficdelay.png │ ├── trhoughput.png │ ├── udpsegment.png │ └── udpsocket.png ├── DSA ├── 00-Data Structures and Applications.md ├── A1-Singly_Linked_List.md ├── A2-Circular_Linked_List.md ├── A3-Doubly_Linked_List.md ├── A4-Circular_Doubly_Linked_List.md ├── A5-Sparse Matrix.md ├── B0-Stack.md ├── B1-Implementation_of_Stack.md ├── B2-Infix_to_Postfix.md ├── B3-Infix_to_Prefix.md ├── B4-Evaluation_of_Expression.md ├── B5-Paranthesis_Matching.md ├── C0-Queue.md ├── C1-Implementation_of_Queue.md ├── C2-Circular_Queue.md ├── C3-Josephus_Problem.md ├── C4-Double_Ended_Queue.md ├── C5-Priority_Queue.md ├── Code │ ├── binary_search_tree.c │ ├── circular_linked_list.c │ ├── circular_queue.c │ ├── double_queue.c │ ├── doubly_circular_linked_list.c │ ├── doubly_linked_list.c │ ├── evaluate_postfix.c │ ├── expression_tree.c │ ├── graph.c │ ├── infix.c │ ├── josephus.c │ ├── linked_list.c │ ├── paranthesis_matching.c │ ├── priority_queue.c │ ├── priority_queue_heap.c │ ├── queue.c │ └── stack.c ├── D0-Tree.md ├── D1-Binary_Tree.md ├── D2-Linked_List_Implementation.md ├── D3-Expression_Tree.md ├── D5-Heap.md ├── D6-Priority_Queue_Heap.md ├── D7-N_ary_Tree.md ├── D8A-AVL_Tree.md ├── D8C-B_Tree.md ├── E0-Graph.md ├── E1-Implementation.md ├── E2-Traversal.md ├── E3-Connectivity.md ├── E4-Network_Topologies.md ├── F0-Hashing.md ├── F1-Seperate_Chaining.md ├── F2-Linear_Probing.md ├── F3-Double_Hashing.md ├── G0-Trie.md └── Images │ ├── dfs.png │ ├── hybrid.png │ ├── left_rotation.png │ ├── lr1.png │ ├── lr2.png │ ├── mesh1.png │ ├── mesh2.png │ ├── multilist1.jpg │ ├── multilist2.jpg │ ├── right_rotation.png │ ├── ring1.png │ ├── ring2.png │ ├── ring3.png │ ├── rl1.png │ ├── rl2.png │ ├── sparse_matrix.png │ ├── star1.png │ ├── start2.png │ └── tree.png ├── Design and Analysis of Algorithms ├── 200-Brute Force.md ├── 201-Exhaustive Search.md ├── 202-Divide and Conquer.md └── Images │ ├── convexhull1.png │ ├── convexhull2.png │ └── master.png ├── Microprocessor and Computer Architecture ├── 100-RISC.md ├── 101-ARM.md ├── 102-Memory Addressing.md ├── 103-Data Processing Instructions.md ├── 104-Branch Instructions.md ├── 105-Load-Store Instructions.md ├── 106-Software Interrupt Instruction.md ├── 107-Program Status Register Instructions.md ├── 108-Function Call.md ├── 109-Instruction Encoding.md ├── 200-Pipeline.md ├── 201-Pipeline Hazards.md ├── 300-Memory.md └── Images │ ├── 3stage.png │ ├── 3stage1.png │ ├── 3stage2.png │ ├── 5stage.png │ ├── Capture.PNG │ ├── addrmode.png │ ├── barrelshift.png │ ├── byteaddr.png │ ├── cpsr.png │ ├── instr1.png │ ├── instr10.png │ ├── instr11.png │ ├── instr12.png │ ├── instr2.png │ ├── instr3.png │ ├── instr4.png │ ├── instr5.png │ ├── instr6.png │ ├── instr7.png │ ├── instr8.png │ ├── instr9.png │ ├── instrusage.png │ ├── memaddr.png │ ├── mul.PNG │ ├── multrans.png │ ├── multrans2.png │ ├── pipeline1.png │ ├── pipeline2.png │ ├── pipeline3.png │ ├── progstruct.png │ ├── psr.png │ ├── psr2.png │ ├── reg.png │ ├── swi.png │ ├── swi2.png │ ├── swi3.png │ └── trans.PNG ├── Operating Systems ├── 300-Main Memory.md ├── 301-Swapping.md ├── 302-Contiguous Allocation.md ├── 303-Segmentation.md ├── 304-Paging.md ├── 305-Structure of Page Table.md └── images │ ├── addr_prot.png │ ├── backingstore.png │ ├── basicmet.png │ ├── codeshare.png │ ├── hashedpage.png │ ├── invertedpage.png │ ├── logphys.png │ ├── memory_space.png │ ├── multistep.png │ ├── pagemodel.png │ ├── pagetable.png │ ├── segmenttable.png │ ├── tlb.png │ ├── twolevel.png │ ├── twolevel1.png │ ├── twolevel2.png │ ├── twolevel3.png │ └── valinval.png ├── README.md └── WebTech ├── 00-Web Development.md ├── A0-HTML&CSS.md ├── A1-Media Queries.md ├── A2-Audio_Video.md ├── A3-Canvas_SVG.md ├── B0-JavaScript.md ├── B1-Objects.md ├── B2-Document_Obejct_Model.md ├── B3-Events.md ├── B31-Page_Load_Events.md ├── B32-Mouse_Events.md ├── B33-Keyboard_Events.md ├── B34-Focus_Events.md ├── B4-Geolocators.md ├── B5-Web_Workers.md ├── B6-Files.md ├── C0-JQuery.md ├── Images ├── complex_component.png ├── dom1.png ├── dom2.png ├── events.png ├── events1.png ├── events2.png ├── events3.png ├── geo1.png ├── mouse1.png └── react_dom.png ├── README.md └── ReactJS ├── Event_Handling.md ├── Form_Handling.md ├── Lifecycle_Methods.md ├── ReactJS.md ├── Stateless_Components.md ├── States.md └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | .obsidian 2 | -------------------------------------------------------------------------------- /.obsidian/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "showInlineTitle": false, 3 | "showLineNumber": true, 4 | "livePreview": true 5 | } -------------------------------------------------------------------------------- /.obsidian/appearance.json: -------------------------------------------------------------------------------- 1 | { 2 | "accentColor": "", 3 | "baseFontSize": 17, 4 | "cssTheme": "Material Gruvbox" 5 | } -------------------------------------------------------------------------------- /.obsidian/community-plugins.json: -------------------------------------------------------------------------------- 1 | [ 2 | "table-editor-obsidian", 3 | "obsidian-git", 4 | "dataview", 5 | "obsidian-admonition" 6 | ] -------------------------------------------------------------------------------- /.obsidian/core-plugins-migration.json: -------------------------------------------------------------------------------- 1 | { 2 | "file-explorer": true, 3 | "global-search": true, 4 | "switcher": true, 5 | "graph": true, 6 | "backlink": true, 7 | "canvas": true, 8 | "outgoing-link": true, 9 | "tag-pane": true, 10 | "page-preview": true, 11 | "daily-notes": true, 12 | "templates": true, 13 | "note-composer": true, 14 | "command-palette": true, 15 | "slash-command": false, 16 | "editor-status": true, 17 | "starred": true, 18 | "markdown-importer": false, 19 | "zk-prefixer": false, 20 | "random-note": false, 21 | "outline": true, 22 | "word-count": true, 23 | "slides": false, 24 | "audio-recorder": false, 25 | "workspaces": false, 26 | "file-recovery": true, 27 | "publish": false, 28 | "sync": false 29 | } -------------------------------------------------------------------------------- /.obsidian/core-plugins.json: -------------------------------------------------------------------------------- 1 | [ 2 | "file-explorer", 3 | "global-search", 4 | "switcher", 5 | "graph", 6 | "backlink", 7 | "canvas", 8 | "outgoing-link", 9 | "tag-pane", 10 | "page-preview", 11 | "daily-notes", 12 | "templates", 13 | "note-composer", 14 | "command-palette", 15 | "editor-status", 16 | "starred", 17 | "outline", 18 | "word-count", 19 | "file-recovery" 20 | ] -------------------------------------------------------------------------------- /.obsidian/graph.json: -------------------------------------------------------------------------------- 1 | { 2 | "collapse-filter": true, 3 | "search": "", 4 | "showTags": false, 5 | "showAttachments": false, 6 | "hideUnresolved": false, 7 | "showOrphans": true, 8 | "collapse-color-groups": true, 9 | "colorGroups": [], 10 | "collapse-display": true, 11 | "showArrow": false, 12 | "textFadeMultiplier": 0, 13 | "nodeSizeMultiplier": 1, 14 | "lineSizeMultiplier": 1, 15 | "collapse-forces": true, 16 | "centerStrength": 0.518713248970312, 17 | "repelStrength": 10, 18 | "linkStrength": 1, 19 | "linkDistance": 250, 20 | "scale": 0.3220461661515744, 21 | "close": false 22 | } -------------------------------------------------------------------------------- /.obsidian/hotkeys.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /.obsidian/plugins/dataview/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "dataview", 3 | "name": "Dataview", 4 | "version": "0.5.53", 5 | "minAppVersion": "0.13.11", 6 | "description": "Complex data views for the data-obsessed.", 7 | "author": "Michael Brenan ", 8 | "authorUrl": "https://github.com/blacksmithgu", 9 | "isDesktopOnly": false 10 | } 11 | -------------------------------------------------------------------------------- /.obsidian/plugins/dataview/styles.css: -------------------------------------------------------------------------------- 1 | /** Live Preview padding fixes, specifically for DataviewJS custom HTML elements. */ 2 | .is-live-preview .block-language-dataviewjs > p, .is-live-preview .block-language-dataviewjs > span { 3 | line-height: 1.0; 4 | } 5 | 6 | .block-language-dataview { 7 | overflow-y: auto; 8 | } 9 | 10 | /*****************/ 11 | /** Table Views **/ 12 | /*****************/ 13 | 14 | /* List View Default Styling; rendered internally as a table. */ 15 | .table-view-table { 16 | width: 100%; 17 | } 18 | 19 | .table-view-table > thead > tr, .table-view-table > tbody > tr { 20 | margin-top: 1em; 21 | margin-bottom: 1em; 22 | text-align: left; 23 | } 24 | 25 | .table-view-table > tbody > tr:hover { 26 | background-color: var(--text-selection) !important; 27 | } 28 | 29 | .table-view-table > thead > tr > th { 30 | font-weight: 700; 31 | font-size: larger; 32 | border-top: none; 33 | border-left: none; 34 | border-right: none; 35 | border-bottom: solid; 36 | 37 | max-width: 100%; 38 | } 39 | 40 | .table-view-table > tbody > tr > td { 41 | text-align: left; 42 | border: none; 43 | font-weight: 400; 44 | max-width: 100%; 45 | } 46 | 47 | .table-view-table ul, .table-view-table ol { 48 | margin-block-start: 0.2em !important; 49 | margin-block-end: 0.2em !important; 50 | } 51 | 52 | /** Rendered value styling for any view. */ 53 | .dataview-result-list-root-ul { 54 | padding: 0em !important; 55 | margin: 0em !important; 56 | } 57 | 58 | .dataview-result-list-ul { 59 | margin-block-start: 0.2em !important; 60 | margin-block-end: 0.2em !important; 61 | } 62 | 63 | /** Generic grouping styling. */ 64 | .dataview.result-group { 65 | padding-left: 8px; 66 | } 67 | 68 | /*******************/ 69 | /** Inline Fields **/ 70 | /*******************/ 71 | 72 | .dataview.inline-field-key { 73 | padding-left: 8px; 74 | padding-right: 8px; 75 | font-family: var(--font-monospace); 76 | background-color: var(--background-primary-alt); 77 | color: var(--text-nav-selected); 78 | } 79 | 80 | .dataview.inline-field-value { 81 | padding-left: 8px; 82 | padding-right: 8px; 83 | font-family: var(--font-monospace); 84 | background-color: var(--background-secondary-alt); 85 | color: var(--text-nav-selected); 86 | } 87 | 88 | .dataview.inline-field-standalone-value { 89 | padding-left: 8px; 90 | padding-right: 8px; 91 | font-family: var(--font-monospace); 92 | background-color: var(--background-secondary-alt); 93 | color: var(--text-nav-selected); 94 | } 95 | 96 | /***************/ 97 | /** Task View **/ 98 | /***************/ 99 | 100 | .dataview.task-list-item, .dataview.task-list-basic-item { 101 | margin-top: 3px; 102 | margin-bottom: 3px; 103 | transition: 0.4s; 104 | } 105 | 106 | .dataview.task-list-item:hover, .dataview.task-list-basic-item:hover { 107 | background-color: var(--text-selection); 108 | box-shadow: -40px 0 0 var(--text-selection); 109 | cursor: pointer; 110 | } 111 | 112 | /*****************/ 113 | /** Error Views **/ 114 | /*****************/ 115 | 116 | div.dataview-error-box { 117 | width: 100%; 118 | min-height: 150px; 119 | display: flex; 120 | align-items: center; 121 | justify-content: center; 122 | border: 4px dashed var(--background-secondary); 123 | } 124 | 125 | .dataview-error-message { 126 | color: var(--text-muted); 127 | text-align: center; 128 | } 129 | 130 | /*************************/ 131 | /** Additional Metadata **/ 132 | /*************************/ 133 | 134 | .dataview.small-text { 135 | font-size: smaller; 136 | color: var(--text-muted); 137 | margin-left: 3px; 138 | } 139 | 140 | .dataview.small-text::before { 141 | content: "("; 142 | } 143 | 144 | .dataview.small-text::after { 145 | content: ")"; 146 | } 147 | -------------------------------------------------------------------------------- /.obsidian/plugins/obsidian-admonition/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "userAdmonitions": {}, 3 | "syntaxHighlight": false, 4 | "copyButton": false, 5 | "version": "9.0.5", 6 | "autoCollapse": false, 7 | "defaultCollapseType": "open", 8 | "injectColor": true, 9 | "parseTitles": true, 10 | "dropShadow": true, 11 | "hideEmpty": false, 12 | "open": { 13 | "admonitions": true, 14 | "icons": true, 15 | "other": true, 16 | "advanced": false 17 | }, 18 | "icons": [], 19 | "useFontAwesome": true, 20 | "rpgDownloadedOnce": false, 21 | "msDocConverted": false, 22 | "useSnippet": false, 23 | "snippetPath": "custom-admonitions.cc57ef" 24 | } -------------------------------------------------------------------------------- /.obsidian/plugins/obsidian-admonition/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "obsidian-admonition", 3 | "name": "Admonition", 4 | "version": "9.0.5", 5 | "minAppVersion": "0.14.0", 6 | "description": "Enhanced callouts for Obsidian.md", 7 | "author": "Jeremy Valentine", 8 | "authorUrl": "", 9 | "isDesktopOnly": false 10 | } 11 | -------------------------------------------------------------------------------- /.obsidian/plugins/obsidian-git/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "commitMessage": "notes update: {{date}}", 3 | "autoCommitMessage": "notes update: {{date}}", 4 | "commitDateFormat": "YYYY-MM-DD", 5 | "autoSaveInterval": 0, 6 | "autoPushInterval": 0, 7 | "autoPullInterval": 0, 8 | "autoPullOnBoot": false, 9 | "disablePush": false, 10 | "pullBeforePush": true, 11 | "disablePopups": false, 12 | "listChangedFilesInMessageBody": true, 13 | "showStatusBar": true, 14 | "updateSubmodules": false, 15 | "syncMethod": "merge", 16 | "customMessageOnAutoBackup": false, 17 | "autoBackupAfterFileChange": false, 18 | "treeStructure": false, 19 | "refreshSourceControl": true, 20 | "basePath": "", 21 | "differentIntervalCommitAndPush": false, 22 | "changedFilesInStatusBar": false, 23 | "showedMobileNotice": true, 24 | "refreshSourceControlTimer": 7000, 25 | "showBranchStatusBar": true, 26 | "setLastSaveToLastCommit": false 27 | } -------------------------------------------------------------------------------- /.obsidian/plugins/obsidian-git/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "obsidian-git", 3 | "name": "Obsidian Git", 4 | "description": "Backup your vault with Git.", 5 | "isDesktopOnly": false, 6 | "fundingUrl": "https://ko-fi.com/vinzent", 7 | "js": "main.js", 8 | "version": "2.16.0" 9 | } 10 | -------------------------------------------------------------------------------- /.obsidian/plugins/table-editor-obsidian/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "formatType": "normal", 3 | "showRibbonIcon": true, 4 | "bindEnter": true, 5 | "bindTab": true 6 | } -------------------------------------------------------------------------------- /.obsidian/plugins/table-editor-obsidian/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "table-editor-obsidian", 3 | "name": "Advanced Tables", 4 | "author": "Tony Grosinger", 5 | "authorUrl": "https://grosinger.net", 6 | "description": "Improved table navigation, formatting, manipulation, and formulas", 7 | "isDesktopOnly": false, 8 | "minAppVersion": "1.0.0", 9 | "version": "0.18.1", 10 | "js": "main.js", 11 | "donation": "https://buymeacoffee.com/tgrosinger" 12 | } -------------------------------------------------------------------------------- /.obsidian/plugins/table-editor-obsidian/styles.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --advanced-tables-helper-size: 28px; 3 | } 4 | 5 | .HyperMD-table-row span.cm-inline-code { 6 | font-size: 100%; 7 | padding: 0px; 8 | } 9 | 10 | .advanced-tables-buttons>div>.title { 11 | font-weight: var(--font-medium); 12 | font-size: var(--nav-item-size); 13 | color: var(--nav-item-color); 14 | text-decoration: underline; 15 | } 16 | 17 | [data-type="advanced-tables-toolbar"] .nav-buttons-container { 18 | column-gap: 0.2rem; 19 | margin: 0.2rem 0 0.2rem 0; 20 | justify-content: start; 21 | } 22 | 23 | [data-type="advanced-tables-toolbar"] .nav-buttons-container::before { 24 | min-width: 2.6rem; 25 | line-height: var(--advanced-tables-helper-size); 26 | font-size: var(--nav-item-size); 27 | font-weight: var(--nav-item-weight); 28 | color: var(--nav-item-color); 29 | } 30 | 31 | [data-type="advanced-tables-toolbar"] .nav-buttons-container>* { 32 | height: var(--advanced-tables-helper-size); 33 | line-height: var(--advanced-tables-helper-size); 34 | } 35 | 36 | [data-type="advanced-tables-toolbar"] .nav-buttons-container .nav-action-button { 37 | width: var(--advanced-tables-helper-size); 38 | height: var(--advanced-tables-helper-size); 39 | display: flex; 40 | justify-content: center; 41 | align-items: center; 42 | border-radius: var(--radius-s); 43 | } 44 | 45 | [data-type="advanced-tables-toolbar"] .nav-buttons-container .nav-action-button:hover { 46 | background-color: var(--nav-item-background-hover); 47 | color: var(--nav-item-color-hover); 48 | font-weight: var(--nav-item-weight-hover); 49 | } 50 | 51 | .advanced-tables-row-label { 52 | width: 50px; 53 | } 54 | 55 | .widget-icon { 56 | width: 20px; 57 | height: 20px; 58 | fill: var(--text-muted); 59 | } 60 | 61 | .widget-icon:hover { 62 | fill: var(--text-normal); 63 | } 64 | 65 | .advanced-tables-csv-export textarea { 66 | height: 200px; 67 | width: 100%; 68 | } 69 | 70 | .advanced-tables-donation { 71 | width: 70%; 72 | margin: 0 auto; 73 | text-align: center; 74 | } 75 | 76 | .advanced-tables-donate-button { 77 | margin: 10px; 78 | } -------------------------------------------------------------------------------- /.obsidian/themes/Material Gruvbox/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Material Gruvbox", 3 | "version": "1.0.0", 4 | "minAppVersion": "0.15.0", 5 | "author": "AllJavi", 6 | "authorUrl": "https://github.com/AllJavi" 7 | } 8 | -------------------------------------------------------------------------------- /.obsidian/themes/Obsidian gruvbox/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Obsidian gruvbox", 3 | "version": "0.1.8", 4 | "minAppVersion": "0.16.0", 5 | "author": "insanum", 6 | "authorUrl": "https://insanum.com" 7 | } 8 | -------------------------------------------------------------------------------- /Computer Networks/100-Internet.md: -------------------------------------------------------------------------------- 1 | # Nuts and Bolts Description 2 | ---- 3 | - Description of the basic hardware and software components that make up the internet []() 4 | 5 | The internet is a computer network that interconnects billions of computing devices throughout the world. These devices can be traditional (pc, workstations) or non-traditional (laptops, smart phones, tvs). All of these devices are called **hosts** or **end systems** 6 | ![[nutsbolts1.png]] 7 | ![nutsbolts1.png](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/nutsbolts1.png) 8 | 9 | ![[nutsbolts2.png]] 10 | ![nutsbolts2.png](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/nutsbolts2.png) 11 | 12 | End systems are connected together by a network of **communication links** and **packet switches** 13 | - Different links are made up of different physical media and can transmit data at different rates (measured in bits/second) 14 | - When one end system sends a message to another, it segments the data and adds header bytes to each segment -> **Packet** 15 | 16 | ## Packet Switch 17 | - Takes a packet arriving on one of its incomming communication links and forwards the packet on one of its outgoing communication links 18 | - Two prominent types: Routers and Link-Layer Switches 19 | - Routers are typically used in the network core and link-layer switches in access networks 20 | - The sequence of communication links and packet switches traversed by a packet from the sending end system to the recieving end system is called the route or path 21 | 22 | ## Internet Service Providers 23 | - End systems access the internet through ISPs including residential ISPs, university ISPs and cellular data ISPs 24 | - Each ISP is a network of packet switches and communication links 25 | - ISPs are also interconnected. The lower-tier ISPs are interconnected through national and internation upper-tier ISPs such as Level 3 Communications, AT&T 26 | - An upper-tier ISP consists of high speed routers interconnected with high speed fiber-optic links 27 | - Each ISP network is managed independently 28 | 29 | ## Protocols 30 | - End systems, packet switches and other pieces of internet run protocols that control the sending and recieving of information within the Internet 31 | - **Transmission Control Protocol** and **Internet Protocol** are two of the most important protocols in the Internet 32 | - **Internet Standards** are developed by the Internet Engineering Task Force. The documents are called **Requests for comments** 33 | - RFCs define protocols such as TCP, IP, HTTP, SMTP 34 | 35 | # Services Description 36 | ---- 37 | - The Internet can also be described as an infrastructure that provides services to applications 38 | - Internet applications include e-mail, web surfing, mobile and tablet applications, online messaging, multiplayer games 39 | - The applications are said to be distributed applications as they involve multiple end systems that exchange data with each other 40 | - Applications run on end systems, not on packet switches 41 | - Packet switches are not concerned with the application that is the source or sink of data 42 | 43 | End systems attached to the Internet provide a **socket interface** that specifies how a program running on one end system asks the Internet infrastructure to deliver data to a specific destination program running on another end system 44 | - The Internet socket interface is a set of rules that the sending - Packet switches are not concerned with the application that is the source or sink of dataprogram must follow so that the Internet can deliver the data to the destination program 45 | 46 | # Protocol 47 | --- 48 | ![[protocol.png]] 49 | ![protocol.png](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/protocol.png) 50 | 51 | A protocol defines the format and the order of messages exchanged between two or more communicating entities, as well as the actions taken on the transmission and/or receipt of a message or other event 52 | - Different protocols are used to accomplish different communication tasks 53 | -------------------------------------------------------------------------------- /Computer Networks/105-Networks Under Attack.md: -------------------------------------------------------------------------------- 1 | # Networks Under Attack 2 | --- 3 | ## Attack on host 4 | - Malware: malicious software, many of which are self-replicating 5 | - Botnet: network of thousands of compromised devices, mainly used for spam email distribution or distributed denial of service attacks 6 | - Viruses: malware that require some form of user interaction to infect the user's device 7 | - Worms: malware that can enter a device without explicit user interaction 8 | 9 | ## Attack on Servers and Network Infrastructure 10 | ### Denial-of-Service 11 | - Attack that renders a network, host, or other piece of infrastructure unusable by legitimate users 12 | - Most Internet DoS attacks fall into one of three categories: 13 | - Vulnerability attack: If the right sequence of packets is sent to a vulnerable application or operating system, the service can stop or the host can crash 14 | - Bandwidth flooding: The attacker sends a deluge of packets to the targeted host, clogging the target's access link and preventing legitimate packets from reaching the server 15 | - Connection flooding: The attacker establishes a large number of half-open or fully open TCP connections to make the host bogged down with bogus connections 16 | 17 | #### Bandwidth flooding 18 | If the server has an access rate of `R` bps, then the attack will need to send traffic at a rate of approximately `R` bps to cause damage. If `R` is very large, a single attack source may not be able to generate enough traffic to harm the server. If all the traffic emanates from a single source, an upstream router may be able to detect the attack and block all the traffic from that source before the traffic gets near the server. 19 | In a Distributed DoS attack, the attacker controls multiple sources and has each source blast traffic at the target. DDoS attacks leverage botnets with thousands of compromised hosts 20 | ![ddos](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/ddos.png) 21 | ![[ddos.png]] 22 | 23 | ## Packet Sniffing 24 | - Packets can contain all kinds of sensitive information, including passwords and private messages 25 | - A passive receiver that records a copy of every packet that flies is called a packet sniffer 26 | - Sniffers can be deployed in both wired and wireless environments 27 | - Sniffers are difficult to detect as they do not inject packets into the channel (they are passive) 28 | - Some of the best defenses against packet sniffing involve cryptography 29 | 30 | ## Identity Theft 31 | - It is surprisingly easy to create a packet with an arbitrary source addres, packet content and destination address, and then transmit this hand-crafted packet into the Internet. This is known as IP spoofing 32 | - End-point authentication is a mechanism that will allow us to determine with certainity if a message originates from where we think it does 33 | -------------------------------------------------------------------------------- /Computer Networks/200-Application Layer.md: -------------------------------------------------------------------------------- 1 | # Network Applications 2 | --- 3 | If there weren't any useful applications, there wouldn't be any need for networking infrastructure and protocols to support them 4 | 5 | - NIC card -> connect to internet, contains MAC address 6 | - Regulated by IEEE 7 | - Switch broadcasts the message to every device connected to the network 8 | -------------------------------------------------------------------------------- /Computer Networks/203-Cookies.md: -------------------------------------------------------------------------------- 1 | # Cookies 2 | --- 3 | - HTTP server is stateless 4 | - It is often desirable for a web server to identify users when it serves content as a function of the user identity 5 | - Cookies allow sites to keep track of users 6 | - Has 4 components: 7 | 1. cookie header line in the HTTP response message 8 | 2. cookie header line in the HTTP request message 9 | 3. cookie file kept on the user's end system and managed by the user's browser 10 | 4. back-end database at the web site 11 | 12 | ![[cookie.png]] 13 | ![cookie](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/cookie.png) 14 | 15 | - The `Set-cookie` header in the HTTP response contains the identification number 16 | - The browser appends a line containing the hostname of the server and the identification number to the special cookie file 17 | - The web server gets to know exactly which pages the user visits, the particular order and also the time at which the page was visited 18 | - If a user registers themselves in the web page with their details, their account details will also be added along with the identification number in the web server's database 19 | - Amazon uses cookies to provide its shopping cart service and also its product recommendation system -------------------------------------------------------------------------------- /Computer Networks/204-Web Cache.md: -------------------------------------------------------------------------------- 1 | # Web Cache 2 | --- 3 | - Web cache is a network entity that satisfies HTTP requests on the behalf of an origin web server 4 | - Also called a proxy server 5 | - It has its own disk storage and keeps copies of recently requested objects in this storage 6 | 7 | 1. The browser establishes a TCP connectionto the web cache and sends an HTTP request for the object to the web cache 8 | 2. The web cache checks to see if it has a copy of the object stored locally. If it does, the web cache returns the object within an HTTP response message to the client browser 9 | 3. If the web cache does not have the object, it opens a TCP connection to the origin server. The web cache then sends an HTTP request for the object into the cache-to-server TCP connection. After receiving this request, the origin server sends the object within an HTTP response to the web cache 10 | 4. When the web cache receives the object, it stores a copy in its local storage and sends a copy, within an HTTP response message, to the client browser (over the existing TCP connection between the client browser and the web cache) 11 | 12 | ![[proxy.png]] 13 | ![proxy](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/proxy.png) 14 | 15 | - Cache is both a server and a client at the same time. When it receives requests from and sends responses to a browser, it is a server. When it sends requests to and receives responses from an origin server, it is a client 16 | 17 | ## Main Functions 18 | - A web cache can substantially reduce the response time for a client request, particularly if the bottleneck bandwidth between the client and the origin server is much less than the bottleneck bandwidth between the client and the cache 19 | - Web caches can substantially reduce traffic in the Interent, improving performance for all applications 20 | 21 | Content Distribution Network (CDN) company installs many geographically distributed caches throughout the Internet, thereby localizing much of the traffic. There are shared CDNs (Akamai and Limelight) and dedicated CDNs (Google and Netflix) 22 | 23 | ## Conditional GET 24 | - The copy of an object residing in the cache may become stale => the object housed in the web serer may have been modified since the copy was cached at the client 25 | - An HTTP request message is called conditional GET message if 26 | - the request message uses the GET method 27 | - the request message includes an `If-Modified-Since:` header line 28 | 29 | 1. On behalf of a requesting browser, a proxy cache sends a request message to a web server 30 | ``` 31 | GET /fruit/kiwi.gif HTTP/1.1 32 | Host: www.exotiquecuisine.com 33 | ``` 34 | 2. The web server sends a response message with the requested object to the cache 35 | ``` 36 | HTTP/1.1 200 OK 37 | Date: Sat, 3 Oct 2015 15:39:29 38 | Server: Apache/1.3.0 (Unix) 39 | Last-Modified: Wed, 9 Sep 2015 09:23:24 40 | Content-Type: image/gif 41 | ``` 42 | 3. The cache forwards the object to the requesting browser, and also caches the object locally. The cache also stores the last-modified date along with the object 43 | 4. After some time, when another browser requests the same object via the cache, and the object is still in cache, the cache performs an up-to-date check by issuing a conditional GET to check whether the object has changed 44 | ``` 45 | GET /fruit/wiki.gif HTTP/1.1 46 | Host: www.exotiquecuisine.com 47 | If-Modified-Since: Web, 9 Sep 2015 09:23:24 48 | ``` 49 | 5. The conditional GET is telling the server to send the object only if the object has been modified since the specified date. If the object has not been modified, the web server sends a response message to the cache 50 | ``` 51 | HTTTP/1.1 304 Not Modified 52 | Date: Sat, 10 Oct 2015 15:39:29 53 | Server: Apache/1.3.0 (Unix) 54 | 55 | (empty entity body) 56 | ``` -------------------------------------------------------------------------------- /Computer Networks/206-DNS Records and Messages.md: -------------------------------------------------------------------------------- 1 | # DNS Records 2 | --- 3 | - The DNS servers that together implement the DNS distributed database store resource records (RR), including RRs that provide hostname-to-IP address mappings 4 | - Each DNS reply message carries one or more resource records 5 | 6 | Resource Recods format: `{name, value, type, ttl}` 7 | - `ttl` is the time to live of the resource record -> determines when a resource should be removed from cache 8 | - `name` and `value` fields depend on `type` 9 | 10 | | Type | Description | 11 | | ----- | ------------------------------------------------------------------------------ | 12 | | A | name is hostname, value is IP address | 13 | | NS | name is domain, value is hostname of authoritative name server for this domain | 14 | | CNAME | name is alias for some 'canonical' name, value is canonical name | 15 | | MX | value is canonical name of a mailserver, associated with alias hostname name | 16 | 17 | - If a DNS server is authoritative for a particular hostname, then the DNS server will contain a Type A record 18 | - If a server is not authoritative for a hostname, then the server will contain a Type NS record as well as a Type A record 19 | 20 | # DNS Messages 21 | --- 22 | - Both query and reply messages have the same format 23 | - `nslookup` program can be used to send a DNS query message directly from your host to some DNS server 24 | 25 | ![[dnsmsg.png]] 26 | ![dnsmsg](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/dnsmsg.png) 27 | 28 | ### Header Section 29 | - The first 12 bytes is the header section. 30 | - The first field is a 16bit number that identifies the query. This identifier is copied into the reply message to a query, allowing the client to match received replies with sent queries 31 | - A 1 bit query/reply flag indicated whether the message is a query(0) or a reply(1) 32 | - A 1 bit authoritative flag is set in a reply message when a DNS server is an authoritative server for a queried name 33 | - A 1 bit recursion-desired flag is set when a client desires that the DNS server perform recursion when it doesn't have the record 34 | - A 1 bit recursion-available flag is set in a reply if the DNS server supports recursion 35 | - There are also four number of fields which indicate the number of occurences of the four types of data sections that follow the header 36 | 37 | ### Question Section 38 | - Contains information about the query that is being made 39 | - Has a name field that contains the name that is being queried 40 | - A type field that indicates the type of question being asked about the name 41 | 42 | ### Answer Section 43 | - Contains the resource records the name that was originally queried 44 | - A reply can return multiple RRs in the answer 45 | 46 | ### Authority Section 47 | - Contains records of other authoritative servers 48 | 49 | ### Additional Section 50 | - Contains other helpful records 51 | - For example, the answer field in a reply to an MX query contains a resource record providing the canonical hostname of a mail server. The additional section contains a Type A record providing the IP address for the canonical hostname of the mail server 52 | 53 | ## Inserting Records 54 | - A registrar is a commerical entity that verifies the uniqueness of the domain name and enters the domain name into the DNS database 55 | - Prior to 1999, Network Solutions had a monopoly on domain name registrations for `com`, `net` and `org` domains 56 | 57 | When registering a domain name, you need to provide the registrar with the names and IP addresses of your primary and secondary authoritative DNS servers. For each of the authoritative DNS servers, the registrar makes sure that a Type NS and a type A record are entered into the TLD servers 58 | 59 | -------------------------------------------------------------------------------- /Computer Networks/207-P2P File Distribution.md: -------------------------------------------------------------------------------- 1 | # Peer to Peer Architecture 2 | --- 3 | ![[p2p.png]] 4 | ![p2p](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/p2p.png) 5 | 6 | - Distribution time is the time it takes get a copy of the file to all N peers 7 | 8 | Distribution time in client-server architecture 9 | - The server must transmit one copy of the file to each of the N peers => server must transmit `NF` bits 10 | - Since the server's upload rate is `u(s)`, the time to distribute the file mustt be atleast `NF/u(s)` 11 | - `d(min)` is the download rate of the peer with the lowest download rate `d(min) = min{d(1), d(2), ..., d(N)}` 12 | 13 | ``` 14 | D(cs) >= max{NF/u(s), F/d(min)} 15 | ``` 16 | 17 | Distribution time in p2p architecture 18 | - The server must send each bit of the file at least once into its access link. The minimum distribution time is at least `F/u(s)`. A bit sent once by the server may not have to be sent by the server again, as the peers may redistribute the bit among themselves 19 | - The peer with the lowest download rate cannot obtain all `F` bits of the file in less than `F/d(min)` seconds 20 | - The total upload capacity of the system as a whole is equal to the upload rate of the server plus the upload rates of the individual peers `u(total) = u(s) + u(1) + u(2) + ... + u(N)`. The system must deliver `F` bits to each of the `N` peers thus delivering a total of `NF` bits 21 | ``` 22 | D(p2p) >= max{F/u(s), F/d(min), NF/u(total)} 23 | ``` 24 | 25 | ![[p2pg.png]] 26 | ![p2pg](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/p2pg.png) 27 | 28 | ## BitTorrent 29 | - Collection of all peers participating in the distribution of a particular file is called a torrent 30 | - Peers in a torrent download equal size chunks of the file from one another, with a typical chunk size of 256 kbytes 31 | - While a peer downloads a chunk, it also uploads chunks to other peers 32 | ![[bittorrent.png]] 33 | ![bittorrent](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/bittorrent.png) 34 | 35 | - Each torrent has an infrastructure node called a tracker 36 | - When a peer joins a torrent, it registers itself with the tracker and periodically informs the tracker that it is still in the torrent 37 | - At any given time, each peer will have a subset of chunks from the file, with different peers having different subsets 38 | - Periodically, the user will ask each of their neighbouring peers (over TCP connections) for the list of chunks they have 39 | - The user issues requests (over TCP) for chunks they currently do not have 40 | - Rarest first technique => determine from among the chunks a user does not have, the chunks that are the rarest among the neighbours and then request those rarest chunks first -> aiming to equalize the number of copeis of each chunk in the torrent 41 | - The user gives priority to the neighbours that are currently supplying them data at the highest rate 42 | - Such neighbours are said to be unchoked and every 10 seconds the rates are recalculated 43 | - Every 30 seconds, the user picks an additional neighbour at random and sends it chunks. Such neighbour is said to be optimistically unchoked 44 | -------------------------------------------------------------------------------- /Computer Networks/300-Introductoin and Transport-Layer Services.md: -------------------------------------------------------------------------------- 1 | # Transport Layer 2 | --- 3 | - A transport layer protocol provides for logical communication between application processes running on different hosts 4 | - Logical -> from an application's perspective, it is as if the hosts running the processes are directly connected; in reality, the hosts may be on the opposite sides of the planet, connected via numerous routers a wide range of link types 5 | - Transport layer protocols are implemented in the end systems, not in the network routers 6 | 7 | On the sending side, the transport layer converts the application layer messages it receives into segments by breaking the application messages into smaller chunks and adding a transport layer header to each chunk. The transport layer passes the segment to the network layer where the segment is encapsulated within a datagram and sent to the destination. On the receiving side, the network layer extracts the segment from the datagram and passes it up to the transport layer, where it is processes and the data is made available to the receiving application 8 | 9 | - A transport layer protocol provides logical communication between processes running on different hosts, a network layer protocol provides logical communication between hosts 10 | - Within an end system, a transport protocol moves messages from application processes to the network edge and vice versa, but it doesn't have any say about how the messages are moved within the network core 11 | 12 | ## Services 13 | - Network layer's IP service model is a best-effort delivery service: the IP makes its best effort to deliver segments between communicating hosts, but it makes no gurantees => IP is said to be an unreliable service 14 | - UDP provides only two services => process-to-process data delivery and error checking. It is also an unreliable service 15 | - TCP provides reliable data transfer and congestion control, along with process-to-process data delivery and error checking -------------------------------------------------------------------------------- /Computer Networks/301-Multiplexing and Demultiplexing.md: -------------------------------------------------------------------------------- 1 | # Multiplexing and Demultiplexing 2 | --- 3 | - A process can have one or more sockets. The transport layer in the receiving host does not deliver data directly to a process, but instead to an intermediary socket 4 | - Each socket has an unique identifier whose format depends on whether the socket is a UDP or a TCP socket 5 | - Each transport layer segment has a set of fields which are examined by the transport layer to identify the receiving socket, and then directs the segment to that socket 6 | - Delivering the data in a transport layer segment to the correct socket is called demultiplexing 7 | - Gathering data chunks at the source host from different sockets, encapsulating each data chunk with header information to create segments, and passing the segments to the network layer is called multiplexing 8 | - Multiplexing requires 9 | - that sockets have unique identifiers 10 | - that each segment has special fields (source port number field and destination port number field) that indicate the destination socket 11 | 12 | ![[port.png]] 13 | ![port](https://github.com/Shogunkayo/PES_Notes/blob/main/Computer%20Networks/Images/port.png) 14 | 15 | - Each port number is a 16-bit number, ranging from 0 to 65535 16 | - The port numbers ranging from 0 to 1023 are called well-known port numbers and are reserved for use by well known application protocols like HTTP and FTP 17 | 18 | ## Connectionless Multiplexing and Demultiplexing 19 | - When creating a socket, the transport layer automatically assigns a port number in the range 1024 to 65535 that is currently not being used by any other UDP port in the host 20 | - `bind()` method can be used to bind a specific port number to the socket 21 | - UDP socket is fully identified by a two-tuple consisting of a destination IP address and a destination port number 22 | - If two UDP segments have different source IP addresses or source port numbers, but the same destination IP address and destination port number, then the two segments will be directed to the same destination process via the same destination socket 23 | - `recvfrom()` method is used to extract the source port number from the segment 24 | 25 | ## Connection Oriented Multiplexing and Demultiplexing 26 | - TCP socket is identified by a four-tuple: source IP address, source port number, destination IP address, destination port number 27 | - Two TCP segments with different source IP addresses or port numbers will be directed to two different sockets (with the exception of a TCP segment carrying the original connection-establishment request) 28 | - The server host may support many simultaneous TCP connection sockets, with each socket attached to a process, and with each socket identified by its own four-tuple 29 | 30 | ## Web Servers 31 | - When clients send segments to the server, all segments will have the same destination port 32 | - There is not always a one-to-one correspondence between connection sockets and processes. Web servers often use only one process, and create a new thread with a new connection socket for each new client connection 33 | - If the connection is persistent, then throughout the duration the client and server exchange HTTP messages via the same server socket -------------------------------------------------------------------------------- /Computer Networks/Images/apparch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/apparch.png -------------------------------------------------------------------------------- /Computer Networks/Images/bittorrent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/bittorrent.png -------------------------------------------------------------------------------- /Computer Networks/Images/cable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/cable.png -------------------------------------------------------------------------------- /Computer Networks/Images/ciruit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/ciruit.png -------------------------------------------------------------------------------- /Computer Networks/Images/cookie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/cookie.png -------------------------------------------------------------------------------- /Computer Networks/Images/ddos.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/ddos.png -------------------------------------------------------------------------------- /Computer Networks/Images/delay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/delay.png -------------------------------------------------------------------------------- /Computer Networks/Images/dns.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/dns.png -------------------------------------------------------------------------------- /Computer Networks/Images/dnsiter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/dnsiter.png -------------------------------------------------------------------------------- /Computer Networks/Images/dnsmsg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/dnsmsg.png -------------------------------------------------------------------------------- /Computer Networks/Images/dnsrecur.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/dnsrecur.png -------------------------------------------------------------------------------- /Computer Networks/Images/dsl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/dsl.png -------------------------------------------------------------------------------- /Computer Networks/Images/ftth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/ftth.png -------------------------------------------------------------------------------- /Computer Networks/Images/gbn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/gbn.png -------------------------------------------------------------------------------- /Computer Networks/Images/gbnfsm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/gbnfsm.png -------------------------------------------------------------------------------- /Computer Networks/Images/gbnop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/gbnop.png -------------------------------------------------------------------------------- /Computer Networks/Images/isp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/isp.png -------------------------------------------------------------------------------- /Computer Networks/Images/multiplexing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/multiplexing.png -------------------------------------------------------------------------------- /Computer Networks/Images/nutsbolts1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/nutsbolts1.png -------------------------------------------------------------------------------- /Computer Networks/Images/nutsbolts2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/nutsbolts2.png -------------------------------------------------------------------------------- /Computer Networks/Images/p2p.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/p2p.png -------------------------------------------------------------------------------- /Computer Networks/Images/p2pg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/p2pg.png -------------------------------------------------------------------------------- /Computer Networks/Images/pipelined.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/pipelined.png -------------------------------------------------------------------------------- /Computer Networks/Images/pipelined2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/pipelined2.png -------------------------------------------------------------------------------- /Computer Networks/Images/port.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/port.png -------------------------------------------------------------------------------- /Computer Networks/Images/protlay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/protlay.png -------------------------------------------------------------------------------- /Computer Networks/Images/protocol.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/protocol.png -------------------------------------------------------------------------------- /Computer Networks/Images/proxy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/proxy.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt10.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt20.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt21r.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt21r.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt21s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt21s.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt22r.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt22r.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt22s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt22s.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt30.png -------------------------------------------------------------------------------- /Computer Networks/Images/rdt30w.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/rdt30w.png -------------------------------------------------------------------------------- /Computer Networks/Images/reliabledata.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/reliabledata.png -------------------------------------------------------------------------------- /Computer Networks/Images/socket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/socket.png -------------------------------------------------------------------------------- /Computer Networks/Images/sr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/sr.png -------------------------------------------------------------------------------- /Computer Networks/Images/srop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/srop.png -------------------------------------------------------------------------------- /Computer Networks/Images/srwin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/srwin.png -------------------------------------------------------------------------------- /Computer Networks/Images/tcpsocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/tcpsocket.png -------------------------------------------------------------------------------- /Computer Networks/Images/tcpsocket2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/tcpsocket2.png -------------------------------------------------------------------------------- /Computer Networks/Images/tcpudp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/tcpudp.png -------------------------------------------------------------------------------- /Computer Networks/Images/tcpvudp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/tcpvudp.png -------------------------------------------------------------------------------- /Computer Networks/Images/traceroute.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/traceroute.png -------------------------------------------------------------------------------- /Computer Networks/Images/trafficdelay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/trafficdelay.png -------------------------------------------------------------------------------- /Computer Networks/Images/trhoughput.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/trhoughput.png -------------------------------------------------------------------------------- /Computer Networks/Images/udpsegment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/udpsegment.png -------------------------------------------------------------------------------- /Computer Networks/Images/udpsocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Computer Networks/Images/udpsocket.png -------------------------------------------------------------------------------- /DSA/00-Data Structures and Applications.md: -------------------------------------------------------------------------------- 1 | # DATA STRUCTURES 2 | --- 3 | 4 | - Data -> raw fact 5 | - Data structure -> arrangement of data with logical relationship between individual elements 6 | - Data must be accessed as fast as possible 7 | 8 | > Algorithm -> Flow chart -> Program 9 | 10 | - Textbook - Data Structures using C/C++, Langsum Yedidyah 2nd edition 11 | 12 | 13 | ## Classification 14 | 15 | 1. Built-in DS - Integer, float, character, pointer 16 | - directly operated upon machine instruction 17 | - all data types are primitive data structures 18 | 19 | 2. User Defined DS - Arrays, Lists, Files 20 | - Grouping of homogenous or heterogenous data types 21 | 1. Linear Data Structures- linked list, stack, queue 22 | 2. Non-Linear Data Structures - tree, graph 23 | 24 | 25 | ## Representation 26 | 27 | 1. Logical - Mathematical - Abstract 28 | 2. Implementation - Code 29 | 30 | One program can have multiple implementations 31 | 32 | 33 | # Static Memory 34 | ---- 35 | 36 | - Memory is assigned before the execution of the program i.e. during the compilation time, in the stack region 37 | - Used when data is fixed and known before processing 38 | 39 | ### Advantages 40 | - Stack region is a more structured memory area 41 | - Faster execution than dynamic memory 42 | - No need of pointers to access data 43 | 44 | ### Disadvantages 45 | - As memory is allocated in compile time, the memory allocated is fixed and cannot be modified 46 | - Can lead to under utilization or over utilization of memory 47 | - Memory cannot be deleted explicitly 48 | - Insertion and deletion of elements to/from intermediate positions is difficult 49 | 50 | 51 | # Dynamic Memory 52 | ---- 53 | - Memory is allocated/deallocated at runtime in the heap region 54 | - stblib.h library is used 55 | 56 | ### Functions 57 | - malloc() - allocates requested size of bytes and returns a void pointer pointing to the first byte of the allocated space 58 | - calloc() - allocates space for an array of elements, initializes them to 0 and then returns a void pointer to the memory 59 | - realloc() - modifies the size of the previously allocated dynamic memory 60 | - free() - releases the memory 61 | 62 | ### Advantages 63 | - Memory is allocated only when the program is unit is active 64 | - Memory is effectively utilized 65 | - Allocated memory can be resized using runtime 66 | 67 | ### Disadvantages 68 | - Heap region is less structured 69 | - Execution is comparatively slower 70 | - Pointers are required to work with dynamically allocated memory 71 | 72 | 73 | 74 | # INDEX 75 | ---- 76 | 77 | ### UNIT 1 - [[A0-Linked_List]] 78 | ### UNIT 2 - [[B0-Stack]] [[C0-Queue]] 79 | -------------------------------------------------------------------------------- /DSA/A4-Circular_Doubly_Linked_List.md: -------------------------------------------------------------------------------- 1 | # CIRCULAR DOUBLY LINKED LIST 2 | ---- 3 | 4 | > Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by previous and next pointer and the last node points to first node by next pointer and also the first node points to last node by the previous pointer. 5 | 6 | ``` 7 | typedef struct node{ 8 | int data; 9 | struct node* next; 10 | struct node* prev; 11 | }node; 12 | ``` 13 | 14 | ## Basic Operations 15 | 16 | #### 1. Insertion 17 | ``` 18 | void insert_start(node** n, int x){ 19 | node* temp = (node*)malloc(sizeof(node)); 20 | temp->data = x; 21 | temp->next = temp; 22 | temp->prev = temp; 23 | 24 | if(*n == NULL){ 25 | *n = temp; 26 | } 27 | else{ 28 | temp->next = (*n)->next; 29 | (*n)->next->prev = temp; 30 | (*n)->next = temp; 31 | temp->prev = (*n); 32 | } 33 | } 34 | ``` 35 | ``` 36 | void insert_end(node** n, int x){ 37 | node* temp = (node*)malloc(sizeof(node)); 38 | temp->data = x; 39 | temp->next = temp; 40 | temp->prev = temp; 41 | 42 | if(*n == NULL){ 43 | *n = temp; 44 | } 45 | else{ 46 | temp->next = (*n)->next; 47 | (*n)->next->prev = temp; 48 | (*n)->next = temp; 49 | temp->prev = (*n); 50 | (*n) = temp; 51 | } 52 | } 53 | ``` 54 | ``` 55 | void insert_pos(node** n, int x, int pos){ 56 | node* temp = (node*)malloc(sizeof(node)); 57 | temp->data = x; 58 | temp->next = temp; 59 | temp->prev = temp; 60 | 61 | if(*n == NULL){ 62 | *n = temp; 63 | } 64 | else{ 65 | node* present = (*n)->next; 66 | 67 | for(int i=1; inext; 69 | } 70 | 71 | temp->prev = present->prev; 72 | present->prev->next = temp; 73 | present->prev = temp; 74 | temp->next = present; 75 | } 76 | 77 | } 78 | ``` 79 | 80 | #### 2. Display 81 | ``` 82 | void display(node** n){ 83 | node* present = (*n)->next; 84 | while(present != *n){ 85 | printf("%d ",present->data); 86 | present = present->next; 87 | } 88 | printf("%d\n",present->data); //To print data of *n 89 | } 90 | ``` 91 | 92 | #### 3. Deletion 93 | ``` 94 | void delete_start(node** n){ 95 | if(*n == NULL){ 96 | printf("Empty List\n"); 97 | } 98 | else{ 99 | node* temp = (*n)->next; 100 | (*n)->next = temp->next; 101 | temp->next->prev = (*n); 102 | free(temp); 103 | } 104 | 105 | } 106 | ``` 107 | ``` 108 | void delete_end(node** n){ 109 | if(*n == NULL){ 110 | printf("Empty List\n"); 111 | } 112 | else{ 113 | node* temp = (*n); 114 | (*n)->prev->next = (*n)->next; 115 | (*n)->next->prev = (*n)->prev; 116 | (*n) = (*n)->prev; 117 | free(temp); 118 | } 119 | } 120 | ``` 121 | ``` 122 | void delete_pos(node** n, int pos){ 123 | if(*n == NULL){ 124 | printf("Empty List\n"); 125 | } 126 | else{ 127 | node* temp = (*n)->next; 128 | node* previous = (*n); 129 | for(int i=1; inext; 132 | } 133 | 134 | previous->next = temp->next; 135 | temp->next->prev = previous; 136 | if(temp == *n){ 137 | *n = previous; 138 | } 139 | free(temp); 140 | } 141 | } 142 | ``` -------------------------------------------------------------------------------- /DSA/A5-Sparse Matrix.md: -------------------------------------------------------------------------------- 1 | # MULTILIST 2 | ---- 3 | 4 | > What is a multilist data structure? Can we call a doubly linked list as a multilist? Mention the applications of a multilist data structure. 5 | - A multilist is a special type of linked list where each node contains two or more pointers 6 | - Doubly linked lists are a special case of Multi-linked lists. It is special in two ways: 7 | - Each node has just 2 pointers. 8 | - The pointers are exact inverses of each other. 9 | - Multilist can be used to represent list of lists 10 | ``` 11 | typedef struct node{ 12 | int data; 13 | struct node* col; 14 | struct node* next; 15 | }node; 16 | ``` 17 | ![list_of_lists](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/multilist1.jpg?raw=true) 18 | 19 | 20 | - Multilist can be used to represent multple orders of a set 21 | 22 | ![set](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/multilist2.jpg?raw=true) 23 | - Another application of multilist is a sparse matrix 24 | 25 | 26 | 27 | # SPARSE MATRIX 28 | ---- 29 | 30 | - A matrix with most of the elements 0 31 | - As there are less non zero elements, memory needed to store them is less 32 | - Computing time can be saved by logically designing a data structure traversing only non-zero elements 33 | 34 | 35 | ## [Representations](https://www.geeksforgeeks.org/sparse-matrix-representation/) 36 | - If a sparse matrix is stored as a 2 dimensional matrix, it wastes a lot of space 37 | - Alternate representations - 38 | 39 | ### 1. Triple notation - Array representation 40 | - Sparse matrix is represented as an array of tuples 41 | - Each tuple consists of row number, column number and value 42 | 43 | ![sparse_matrix](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/sparse_matrix.png?raw=true) 44 | 45 | ``` 46 | typedef struct sparse{ 47 | int col; 48 | int row; 49 | int data; 50 | }term; 51 | ``` 52 | 53 | ### 2. Linked List representation 54 | - Two types of nodes are used - header node and data node 55 | - Header node consists of pointers to next, down and right 56 | - Data node consists of row number, column number, value and the pointers to down and right 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /DSA/B0-Stack.md: -------------------------------------------------------------------------------- 1 | # STACK 2 | ---- 3 | 4 | - Last in First out 5 | - Can be implemented by using arrays or linked list 6 | - Overflow - stack is full 7 | - Underflow - stack is empty 8 | 9 | 10 | ### ADT for Stack: 11 | | Function | Function name | 12 | | ----------------------- | ------------- | 13 | | Insert | push | 14 | | Delete | pop | 15 | | Check if stack is full | isFull | 16 | | Check if stack is empty | isEmpty | 17 | | Return top most | peek | 18 | 19 | 20 | ### Implementations: 21 | 1. Array method 22 | 2. Linked list method 23 | 24 | [[B1-Implementation_of_Stack]] 25 | 26 | ### Applications: 27 | - Recursion 28 | - Conversion of an infix expression to postfix: [[B2-Infix_to_Postfix]] prefix: [[B3-Infix_to_Prefix]] 29 | - Evaluation of expression: [[B4-Evaluation_of_Expression]] 30 | - Paranthesis matching: [[B5-Paranthesis_Matching]] 31 | - Syntax parsing 32 | - Backtracking - pathfinding 33 | 34 | 35 | ### Question 36 | [Standard Problems on Stacks](https://www.geeksforgeeks.org/next-greater-element/?ref=lbp) 37 | 38 | 39 | -------------------------------------------------------------------------------- /DSA/B1-Implementation_of_Stack.md: -------------------------------------------------------------------------------- 1 | # IMPLEMENTATION OF STACK 2 | ----- 3 | 4 | ## Using Array 5 | 6 | #### 1. Insert 7 | ``` 8 | void push(char* s, int* t, char ch, int size){ 9 | if(*t == size-1){ 10 | printf("Stack Overflow\n"); 11 | } 12 | else{ 13 | s[++(*t)] = ch; 14 | } 15 | 16 | } 17 | ``` 18 | 19 | #### 2. Delete 20 | ``` 21 | char pop(char* s, int* t, int size){ 22 | if(*t == -1){ 23 | printf("Stack Underflow\n"); 24 | } 25 | else{ 26 | return s[(*t)--]; 27 | } 28 | } 29 | ``` 30 | 31 | #### 3. Display 32 | ``` 33 | void display(char* s, int* t){ 34 | if(*t == -1){ 35 | printf("Stack Underflow\n"); 36 | } 37 | else{ 38 | for(int i=*t; i>-1; i--){ 39 | printf("%c ",s[i]); 40 | } 41 | } 42 | printf("\n"); 43 | } 44 | ``` 45 | 46 | #### 4. Peek 47 | ``` 48 | char peek(char* s, int* t){ 49 | return s[*t]; 50 | } 51 | ``` 52 | 53 | 54 | -------------------------------------------------------------------------------- /DSA/B2-Infix_to_Postfix.md: -------------------------------------------------------------------------------- 1 | # POSTFIX EXPRESSION 2 | --- 3 | 4 | - Postfix expression is an expression where the operator is placed after the two operands 5 | 6 | A+B = AB+ 7 | 8 | - To evaluate an infix expression, the compiler scans the expression repeatedly untill all the operations are performed 9 | - Evaluating a postfix/prefix expression is more efficient 10 | 11 | ## Algorithm 12 | 1. Scan the infix expression from left to right 13 | 2. As long as the stack precedence value of the symbol on the top of the stack is greater than the input precedence value of the input symbol, pop the character and place it in the postfix array. If it fails, proceed to the next step 14 | 3. If the stack precedence value is not equal to the input precedence value, push the current value onto the stack. Else delete the item from the stack. 15 | 4. Repeat untill the string ends 16 | 17 | | Stack Precedence | Value | 18 | | ----------------- | ----- | 19 | | +, - | 2 | 20 | | /,* | 4 | 21 | | $ | 5 | 22 | | ( | 0 | 23 | | # | -1 | 24 | | default(operands) | 8 | 25 | 26 | | Input Precedence | Value | 27 | | ----------------- | ----- | 28 | | +, - | 1 | 29 | | /, * | 3 | 30 | | $ | 6 | 31 | | ( | 9 | 32 | | ) | 0 | 33 | | default(operands) | 7 | 34 | 35 | ## Implementation 36 | ``` 37 | void push(char* s, int* t, char ch){ 38 | s[++(*t)] = ch; 39 | } 40 | 41 | char peek(char* s, int* t){ 42 | return s[*t]; 43 | } 44 | 45 | char pop(char* s, int* t){ 46 | return s[(*t)--]; 47 | } 48 | 49 | int stkprec(char ch){ 50 | switch(ch){ 51 | case '+': 52 | case '-': return 2; 53 | 54 | case '*': 55 | case '/': return 4; 56 | 57 | case '$': return 5; 58 | 59 | case '(': return 0; 60 | 61 | case '#': return -1; 62 | 63 | default: return 8; 64 | } 65 | } 66 | 67 | int ipprec(char ch){ 68 | switch(ch){ 69 | case '+': 70 | case '-': return 1; 71 | 72 | case '*': 73 | case '/': return 3; 74 | 75 | case '$': return 6; 76 | 77 | case '(': return 9; 78 | 79 | case ')': return 0; 80 | 81 | default: return 7; 82 | } 83 | } 84 | 85 | void infix_postfix(char* infix, char* postfix){ 86 | char s[20]; 87 | int top=-1, i=0, j=0; 88 | 89 | push(s,&top,'#'); 90 | 91 | while(infix[i]){ 92 | while(stkprec(peek(s,&top)) > ipprec(infix[i])){ 93 | postfix[j++] = pop(s,&top); 94 | } 95 | 96 | if(stkprec(peek(s,&top)) < ipprec(infix[i])){ 97 | push(s,&top,infix[i]); 98 | } 99 | else{ 100 | pop(s,&top); 101 | } 102 | 103 | i++; 104 | } 105 | 106 | while(peek(s,&top)!='#'){ 107 | postfix[j++]=pop(s,&top); 108 | postfix[j]='\0'; 109 | } 110 | 111 | } 112 | ``` 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /DSA/B3-Infix_to_Prefix.md: -------------------------------------------------------------------------------- 1 | # PREFIX EXPRESSION 2 | --- 3 | 4 | - Prefix expression is an expression where the operator is placed after the two operands 5 | 6 | A+B = +AB 7 | 8 | ## Algorithm 9 | 1. Reverse the input string 10 | 2. Change all '(' to ')' and vice versa 11 | 3. Find the postfix expression of the reversed infix 12 | 4. Reverse the above obtained string to get the prefix expression 13 | 14 | ## Implementation 15 | ``` 16 | void strrev(char* s){ 17 | int len = strlen(s); 18 | char temp; 19 | for(int i=0; i-1;i--){ 75 | if(is_operator(prefix[i])){ 76 | op1 = int_pop(s,&top); 77 | op2 = int_pop(s,&top); 78 | switch(prefix[i]){ 79 | case '+': int_push(s,&top,op1+op2); 80 | break; 81 | case '-': int_push(s,&top,op1-op2); 82 | break; 83 | case '*': int_push(s,&top,op1*op2); 84 | break; 85 | case '/': int_push(s,&top,op1/op2); 86 | break; 87 | /* 88 | case '$': push(s,&top,pow(op1,op2)); 89 | break; 90 | */ 91 | } 92 | } 93 | else{ 94 | 95 | printf("%c: ",prefix[i]); 96 | scanf("%d", &a); 97 | int_push(s,&top,a); 98 | } 99 | } 100 | 101 | return(int_pop(s,&top)); 102 | } 103 | ``` 104 | 105 | -------------------------------------------------------------------------------- /DSA/B5-Paranthesis_Matching.md: -------------------------------------------------------------------------------- 1 | # PARANTHESIS MATCHING 2 | ---- 3 | 4 | -> Matching - (), {}, [()], [({})], [(){}] 5 | -> Not Matching - ({), (, ], {[}] 6 | 7 | ## Algorithm 8 | 1. Declare an empty stack. 9 | 2. Traverse the input string 10 | 3. If the character is an opening paranthesis, push it onto the stack 11 | 4. If the character is a closing paranthesis, provided the stack is not empty, pop the top of the stack and compare. 12 | 5. If they match, return 1 (successful) 13 | 6. Else return 0(unsuccessful) 14 | 7. After traversal, if there are any other paranthesis left in the stack, return 0 15 | 16 | ## Implementation 17 | ``` 18 | int compare(char* s, int* t, char x){ 19 | char popped; 20 | if(*t == -1){ 21 | return 0; 22 | } 23 | else{ 24 | popped = pop(s,t); 25 | switch(x){ 26 | case ')': if(popped == '(') return 1; 27 | case '}': if(popped == '{') return 1; 28 | case ']': if(popped == '[') return 1; 29 | } 30 | return 0; 31 | } 32 | } 33 | 34 | int par_match(char* e){ 35 | char s[20]; 36 | int top=-1; 37 | int i=0; 38 | int match=1; 39 | 40 | while(e[i] != '\0'){ 41 | switch(e[i]){ 42 | case '(': 43 | case '[': 44 | case '{': 45 | push(s,&top,e[i]); 46 | break; 47 | case ')': 48 | case ']': 49 | case '}': 50 | match = compare(s,&top,e[i]); 51 | break; 52 | default: return -1; 53 | } 54 | if(!match){ 55 | return 0; 56 | } 57 | i++; 58 | } 59 | if(top != -1){ 60 | return 0; 61 | } 62 | else{ 63 | return 1; 64 | } 65 | } 66 | ``` -------------------------------------------------------------------------------- /DSA/C0-Queue.md: -------------------------------------------------------------------------------- 1 | # QUEUE 2 | --- 3 | 4 | - First in First out 5 | - Insertion always happens at the end, and deletion at the start 6 | - Two pointers - head and last are used 7 | 8 | ### ADT for Stack: 9 | | Function | Function name | 10 | | ------------------------ | ------------- | 11 | | Insert | enqueue | 12 | | Delete | dequeue | 13 | | Check if queue is empty | isempty | 14 | | Check if queue is full | isfull | 15 | | Return the start element | peek | 16 | 17 | ### Implementation: 18 | 1. Arrays 19 | 2. Linked List 20 | 21 | [[C1-Implementation_of_Queue]] 22 | 23 | ### Applications: 24 | 1. Disk Scheduling 25 | 2. CPU Scheduling 26 | 3. Batch Processing 27 | 28 | ### Types: 29 | 1. Circular Queue: [[C2-Circular_Queue]] [[C3-Josephus_Problem]] 30 | 2. Double ended Queue: [[C4-Double_Ended_Queue]] 31 | 3. Priority Queue: [[C5-Priority_Queue]] 32 | -------------------------------------------------------------------------------- /DSA/C1-Implementation_of_Queue.md: -------------------------------------------------------------------------------- 1 | # IMPLEMENTATION OF QUEUE 2 | --- 3 | 4 | ## Using Arrays 5 | #### 1. Insert 6 | ``` 7 | void enqueue(int* q, int* f, int* r, int size, int x){ 8 | if(*r == size-1){ 9 | printf("Queue Overflow\n"); 10 | } 11 | else{ 12 | q[++(*r)] = x; 13 | } 14 | if(*f == -1){ 15 | *f=0; 16 | } 17 | } 18 | ``` 19 | 20 | #### 2. Display 21 | ``` 22 | void display(int* q, int* f, int* r){ 23 | if(*f == -1){ 24 | printf("Queue Underflow\n"); 25 | } 26 | for(int i=*f; i <= *r; i++){ 27 | printf("%d ",q[i]); 28 | } 29 | printf("\n"); 30 | } 31 | ``` 32 | 33 | #### 3. Delete 34 | ``` 35 | int dequeue(int* q, int* f, int* r){ 36 | if(*f == -1){ 37 | printf("Queue Underflow\n"); 38 | return -1; 39 | } 40 | else{ 41 | int x = q[*f]; 42 | if(*f == *r){ 43 | f = r = -1; 44 | } 45 | else{ 46 | (*f++); 47 | } 48 | return x; 49 | } 50 | } 51 | ``` 52 | 53 | 54 | ## Using Linked Lists 55 | - Two pointers are used : one pointer at the front and another at the rear 56 | 57 | #### 1. Insert 58 | ``` 59 | void enqueue(node** f, node** r, int x){ 60 | node* temp = (node*)malloc(sizeof(node)); 61 | temp->data = x; 62 | temp->next = NULL; 63 | 64 | if(*f == NULL){ 65 | *f = *r = temp; 66 | } 67 | else{ 68 | (*r)->next = temp; 69 | (*r) = temp; 70 | } 71 | } 72 | ``` 73 | 74 | #### 2. Display 75 | ``` 76 | void display(node** f, node** r){ 77 | node* q = *f; 78 | if(q==NULL){ 79 | printf("Queue is empty\n"); 80 | } 81 | else{ 82 | while(q != *r){ 83 | printf("%d ",q->data); 84 | } 85 | printf("%d\n",q->data); 86 | } 87 | } 88 | ``` 89 | 90 | #### 3. Delete 91 | ``` 92 | void dequeue(node** f, node** r){ 93 | node* temp = *f; 94 | if(temp == NULL){ 95 | printf("Queue is empty\n"); 96 | } 97 | else{ 98 | *f = (*f)->next; 99 | free(temp); 100 | } 101 | } 102 | ``` 103 | 104 | ## Questions 105 | >Write a program to implement stack using queues. 106 | 107 | > Write a program to implement queue using stacks. 108 | 109 | -------------------------------------------------------------------------------- /DSA/C2-Circular_Queue.md: -------------------------------------------------------------------------------- 1 | # CIRCULAR QUEUE 2 | --- 3 | - In a regular queue, elements cannot be inserted when a queue is full, even if some elements are deleted afterwards 4 | - A circular queue is used to overcome this drawback 5 | 6 | ## Operations 7 | #### 1. Insert 8 | ``` 9 | void enqueue(int* q, int* f, int* r, int size, int x){ 10 | if((*r)+ 1 %size == *f){ 11 | printf("Queue is full\n"); 12 | } 13 | else{ 14 | *r = (*r)+1 % size; 15 | q[*r] = x; 16 | if(*f == -1){ 17 | *f = 0; 18 | } 19 | } 20 | } 21 | 22 | ``` 23 | 24 | #### 2. Display 25 | ``` 26 | void display(int* q, int f, int r, int size){ 27 | if(f == -1){ 28 | printf("Queue is empty\n"); 29 | } 30 | else{ 31 | while(f != r){ 32 | printf("%d ",q[f]); 33 | f = (f+1)%size; 34 | } 35 | printf("%d\n",q[f]); 36 | } 37 | } 38 | ``` 39 | 40 | #### 3. Delete 41 | ``` 42 | int dequeue(int* q, int* f, int* r, int size){ 43 | if(*f == -1){ 44 | printf("Queue is empty\n"); 45 | return -1; 46 | } 47 | else{ 48 | int x = q[*f]; 49 | if(*f == *r){ 50 | *f = *r = -1; 51 | } 52 | else{ 53 | *f = (*f)+1 %size; 54 | } 55 | return x; 56 | } 57 | } 58 | ``` 59 | 60 | ## Applications 61 | 1. Memory Management 62 | 2. Traffic System 63 | 3. CPU Scheduling 64 | 65 | ## Questions 66 | >Implement enqueue and dequeue operations of a circular queue using an array Q of size n. Assume f is front and r is the next available cell in the array 67 | 68 | > Write a method called 'alternate_split' to split an integer queue into two queues which contains alternate elements of the original queue. Use only queue methods that are usually defined in the queue ADT (enqueue, dequeue). For example, if the original queue has {1,2,3,4,5}, then Q1 = {1,3,5} and Q2 = (2,4). You do not have to write enqueue and dequeue methods 69 | -------------------------------------------------------------------------------- /DSA/C3-Josephus_Problem.md: -------------------------------------------------------------------------------- 1 | # JOSEPHUS PROBLEM USING CIRCULAR QUEUE 2 | --- 3 | 4 | ## Problem Statement: 5 | >There are n people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of person n and a number k which indicates that k-1 persons are skipped and the kth person is killed in a circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive. 6 | 7 | ## Code: 8 | ``` 9 | #include 10 | #include 11 | 12 | typedef struct Node{ 13 | int position; 14 | struct Node* link; 15 | }node; 16 | 17 | void insert(node** n, int x){ 18 | node* temp = (node*)malloc(sizeof(node)); 19 | temp->position = x; 20 | temp->link = temp; 21 | 22 | if(*n == NULL){ 23 | *n = temp; 24 | } 25 | else{ 26 | temp->link = (*n)->link; 27 | (*n)->link = temp; 28 | } 29 | } 30 | 31 | int josephus(node** n, int k){ 32 | node* present = (*n)->link; 33 | node* previous = *n; 34 | int i=1; 35 | while(present->link != present){ 36 | if(i%k == 0){ 37 | previous->link = present->link; 38 | free(present); 39 | present = previous->link; 40 | } 41 | else{ 42 | previous = present; 43 | present = present->link; 44 | } 45 | i++; 46 | } 47 | return present->position; 48 | } 49 | 50 | int main(){ 51 | node* head = NULL; 52 | int n=10, k=4; 53 | for(int i=n; i>0; i--){ 54 | insert(&head, i); 55 | } 56 | printf("%d\n",josephus(&head,k)); 57 | } 58 | ``` 59 | -------------------------------------------------------------------------------- /DSA/C4-Double_Ended_Queue.md: -------------------------------------------------------------------------------- 1 | # DOUBLE ENDED QUEUE 2 | --- 3 | - Type of queue that allows insertion and deletion at both ends 4 | - Does not follow FIFO 5 | - Also called "Deque" 6 | 7 | - Supports both stack and queue operations 8 | - Supports clockwise and anti-clockwise rotations in constant time 9 | 10 | ## Applications 11 | - 12 | 13 | ## ADT 14 | - insert_head 15 | - insert_tail 16 | - delete_head 17 | - delete_tail 18 | - isEmpty 19 | - isFull 20 | - return_front 21 | - return_rear 22 | 23 | ## Implementation 24 | ``` 25 | typedef struct Node{ 26 | int data; 27 | struct Node* prev; 28 | struct Node* next; 29 | }node; 30 | 31 | typedef struct Deque{ 32 | node* front; 33 | node* rear; 34 | }deque; 35 | ``` 36 | 37 | ## Basic Operations 38 | 39 | #### 1. Insert 40 | ``` 41 | void insert_front(deque* dq, int x){ 42 | node* temp = (node*)malloc(sizeof(node)); 43 | temp->data = x; 44 | temp->prev = NULL; 45 | temp->next = NULL; 46 | 47 | if(dq->front == NULL){ 48 | dq->front = temp; 49 | dq->rear = temp; 50 | } 51 | else{ 52 | temp->next = dq->front; 53 | dq->front->prev = temp; 54 | dq->front = temp; 55 | } 56 | } 57 | ``` 58 | 59 | ``` 60 | void insert_last(deque* dq, int x){ 61 | node* temp = (node*)malloc(sizeof(node)); 62 | temp->data = x; 63 | temp->next = NULL; 64 | temp->prev = NULL; 65 | 66 | if(dq->front == NULL){ 67 | dq->front = temp; 68 | dq->rear = temp; 69 | } 70 | else{ 71 | dq->rear->next = temp; 72 | temp->prev = dq->rear; 73 | dq->rear = temp; 74 | } 75 | } 76 | ``` 77 | 78 | #### 2. Display 79 | ``` 80 | void display(deque* dq){ 81 | node* temp = dq->front; 82 | while(temp != NULL){ 83 | printf("%d ",temp->data); 84 | temp = temp->next; 85 | } 86 | printf("\n"); 87 | } 88 | ``` 89 | 90 | #### 3. Delete 91 | ``` 92 | void delete_front(deque* dq){ 93 | if(dq->front == NULL){ 94 | printf("Empty Queue\n"); 95 | } 96 | else{ 97 | node* temp = dq->front; 98 | dq->front = dq->front->next; 99 | // Condition to catch last remaining node 100 | if(dq->front != NULL){ 101 | dq->front->prev = NULL; 102 | } 103 | else{ 104 | dq->front = NULL; 105 | dq->rear = NULL; 106 | } 107 | free(temp); 108 | } 109 | } 110 | ``` 111 | 112 | ``` 113 | void delete_rear(deque* dq){ 114 | if(dq->front == NULL){ 115 | printf("Empty Queue\n"); 116 | } 117 | else{ 118 | node* temp = dq->rear; 119 | dq->rear = dq->rear->prev; 120 | if(dq->rear != NULL){ 121 | dq->rear->next = NULL; 122 | } 123 | else{ 124 | dq->front = NULL; 125 | dq->rear = NULL; 126 | } 127 | free(temp); 128 | } 129 | } 130 | ``` 131 | 132 | 133 | -------------------------------------------------------------------------------- /DSA/C5-Priority_Queue.md: -------------------------------------------------------------------------------- 1 | # PRIORITY QUEUE 2 | --- 3 | 4 | >Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed). If in any case the elements have same priority, they are served as per their ordering in the queue. 5 | 6 | ## Applications 7 | 1. Dijskstra’s shortest path algorithm 8 | 2. Prims algorithm 9 | 3. Artificial Intelligence - search algorithm 10 | 4. Data compression in Huffman code 11 | 12 | ## Implementation 13 | ### 1. Using Arrays 14 | ``` 15 | typedef struct priority_queue{ 16 | int data; 17 | int priority; 18 | }queue; 19 | 20 | void insert(queue *pq, int* count, int x, int p){ 21 | int j = *count - 1; 22 | queue temp; 23 | temp.data = x; 24 | temp.priority = p; 25 | 26 | while(j>=0 && pq[j].priority > temp.priority){ 27 | pq[j+1] = pq[j]; 28 | j--; 29 | } 30 | 31 | pq[j+1] = temp; 32 | (*count)++; 33 | } 34 | 35 | void display(queue *pq, int count){ 36 | if(count){ 37 | for(int i=0; i -1){ 73 | for(int i=0; i max){ 86 | max = pq[i].priority; 87 | } 88 | } 89 | 90 | // +1 to take care of deleting the highest priority node 91 | return max+1; 92 | } 93 | 94 | void delete(queue *pq, int* count){ 95 | if(*count == -1){ 96 | printf("Empty Queue\n"); 97 | } 98 | else{ 99 | queue temp; 100 | temp.priority = max_priority(pq, *count); 101 | int index = -1; 102 | 103 | for(int i=0; i<*count+1; i++){ 104 | if(pq[i].priority < temp.priority){ 105 | temp = pq[i]; 106 | index = i; 107 | } 108 | } 109 | 110 | for(int i=index; i<*count; i++){ 111 | pq[i] = pq[i+1]; 112 | } 113 | 114 | (*count)--; 115 | } 116 | } 117 | ``` 118 | -------------------------------------------------------------------------------- /DSA/Code/circular_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void enqueue(int* q, int* f, int* r, int size, int x){ 4 | if(((*r)+1)%size == *f){ 5 | printf("Queue is full\n"); 6 | } 7 | else{ 8 | *r = ((*r)+1) % size; 9 | q[*r] = x; 10 | if(*f == -1){ 11 | *f = 0; 12 | } 13 | } 14 | } 15 | 16 | void display(int* q, int f, int r, int size){ 17 | if(f == -1){ 18 | printf("Queue is empty\n"); 19 | } 20 | else{ 21 | while(f != r){ 22 | printf("%d ",q[f]); 23 | f = (f+1)%size; 24 | } 25 | printf("%d\n",q[f]); 26 | } 27 | } 28 | 29 | int dequeue(int* q, int* f, int* r, int size){ 30 | if(*f == -1){ 31 | printf("Queue is empty\n"); 32 | return -1; 33 | } 34 | else{ 35 | int x = q[*f]; 36 | if(*f == *r){ 37 | *f = *r = -1; 38 | } 39 | else{ 40 | *f = ((*f)+1) %size; 41 | } 42 | return x; 43 | } 44 | } 45 | 46 | int main(){ 47 | int a[5]; 48 | int f=-1, r=-1, size=5; 49 | enqueue(a, &f, &r, 5, 10); 50 | display(a, f, r, size); 51 | enqueue(a, &f, &r, 5, 20); 52 | enqueue(a, &f, &r, 5, 30); 53 | display(a, f, r, size); 54 | dequeue(a, &f, &r, size); 55 | display(a, f, r, size); 56 | printf("%d %d\n",f,r); 57 | enqueue(a, &f, &r, 5, 30); 58 | printf("%d %d\n",f,r); 59 | display(a, f, r, size); 60 | enqueue(a, &f, &r, 5, 30); 61 | printf("%d %d\n",f,r); 62 | enqueue(a, &f, &r, 5, 30); 63 | printf("%d %d\n",f,r); 64 | enqueue(a, &f, &r, 5, 30); 65 | display(a, f, r, size); 66 | } 67 | 68 | // Implement enqueue and dequeue operations of a circular queue using an array Q 69 | // of size n. Assume f is front and r is the next available cell in the array 70 | 71 | // Write a method called 'alternate_split' to split an integer queue into two queues 72 | // which contains alternate elements of the original queue. Use only queue methods 73 | // that are usually defined in the queue ADT (enqueue, dequeue). For example, if 74 | // the original queue has {1,2,3,4,5}, then Q1 = {1,3,5} and Q2 = (2,4). You do not 75 | // have to write enqueue and dequeue methods 76 | 77 | // Write a program to implement stack using queues. 78 | 79 | // Write a program to implement queue using stacks. -------------------------------------------------------------------------------- /DSA/Code/double_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node{ 5 | int data; 6 | struct Node* prev; 7 | struct Node* next; 8 | }node; 9 | 10 | typedef struct Deque{ 11 | node* front; 12 | node* rear; 13 | }deque; 14 | 15 | void insert_front(deque* dq, int x){ 16 | node* temp = (node*)malloc(sizeof(node)); 17 | temp->data = x; 18 | temp->prev = NULL; 19 | temp->next = NULL; 20 | 21 | if(dq->front == NULL){ 22 | dq->front = temp; 23 | dq->rear = temp; 24 | } 25 | else{ 26 | temp->next = dq->front; 27 | dq->front->prev = temp; 28 | dq->front = temp; 29 | } 30 | } 31 | 32 | void insert_last(deque* dq, int x){ 33 | node* temp = (node*)malloc(sizeof(node)); 34 | temp->data = x; 35 | temp->next = NULL; 36 | temp->prev = NULL; 37 | 38 | if(dq->front == NULL){ 39 | dq->front = temp; 40 | dq->rear = temp; 41 | } 42 | else{ 43 | dq->rear->next = temp; 44 | temp->prev = dq->rear; 45 | dq->rear = temp; 46 | } 47 | } 48 | 49 | void display(deque* dq){ 50 | node* temp = dq->front; 51 | while(temp != NULL){ 52 | printf("%d ",temp->data); 53 | temp = temp->next; 54 | } 55 | printf("\n"); 56 | } 57 | 58 | void delete_front(deque* dq){ 59 | if(dq->front == NULL){ 60 | printf("Empty Queue\n"); 61 | } 62 | else{ 63 | node* temp = dq->front; 64 | dq->front = dq->front->next; 65 | // Condition to catch last remaining node 66 | if(dq->front != NULL){ 67 | dq->front->prev = NULL; 68 | } 69 | else{ 70 | dq->front = NULL; 71 | dq->rear = NULL; 72 | } 73 | free(temp); 74 | } 75 | } 76 | 77 | void delete_rear(deque* dq){ 78 | if(dq->front == NULL){ 79 | printf("Empty Queue\n"); 80 | } 81 | else{ 82 | node* temp = dq->rear; 83 | dq->rear = dq->rear->prev; 84 | if(dq->rear != NULL){ 85 | dq->rear->next = NULL; 86 | } 87 | else{ 88 | dq->front = NULL; 89 | dq->rear = NULL; 90 | } 91 | free(temp); 92 | } 93 | } 94 | 95 | int main(){ 96 | deque dq; 97 | dq.front = NULL; 98 | dq.rear = NULL; 99 | insert_front(&dq, 10); 100 | insert_front(&dq, 20); 101 | insert_front(&dq, 30); 102 | insert_last(&dq, 100); 103 | insert_last(&dq, 200); 104 | insert_front(&dq, 1000); 105 | display(&dq); 106 | delete_front(&dq); 107 | delete_front(&dq); 108 | display(&dq); 109 | delete_rear(&dq); 110 | delete_rear(&dq); 111 | delete_rear(&dq); 112 | display(&dq); 113 | delete_rear(&dq); 114 | display(&dq); 115 | delete_front(&dq); 116 | delete_rear(&dq); 117 | delete_rear(&dq); 118 | delete_front(&dq); 119 | insert_last(&dq, 100); 120 | insert_last(&dq, 200); 121 | insert_front(&dq, 1000); 122 | display(&dq); 123 | } -------------------------------------------------------------------------------- /DSA/Code/doubly_circular_linked_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct node{ 5 | int data; 6 | struct node* next; 7 | struct node* prev; 8 | }node; 9 | 10 | void insert_start(node** n, int x){ 11 | node* temp = (node*)malloc(sizeof(node)); 12 | temp->data = x; 13 | temp->next = temp; 14 | temp->prev = temp; 15 | 16 | if(*n == NULL){ 17 | *n = temp; 18 | } 19 | else{ 20 | temp->next = (*n)->next; 21 | (*n)->next->prev = temp; 22 | (*n)->next = temp; 23 | temp->prev = (*n); 24 | } 25 | } 26 | 27 | void insert_end(node** n, int x){ 28 | node* temp = (node*)malloc(sizeof(node)); 29 | temp->data = x; 30 | temp->next = temp; 31 | temp->prev = temp; 32 | 33 | if(*n == NULL){ 34 | *n = temp; 35 | } 36 | else{ 37 | temp->next = (*n)->next; 38 | (*n)->next->prev = temp; 39 | (*n)->next = temp; 40 | temp->prev = (*n); 41 | (*n) = temp; 42 | } 43 | } 44 | 45 | void insert_pos(node** n, int x, int pos){ 46 | node* temp = (node*)malloc(sizeof(node)); 47 | temp->data = x; 48 | temp->next = temp; 49 | temp->prev = temp; 50 | 51 | if(*n == NULL){ 52 | *n = temp; 53 | } 54 | else{ 55 | node* present = (*n)->next; 56 | 57 | for(int i=1; inext; 59 | } 60 | 61 | temp->prev = present->prev; 62 | present->prev->next = temp; 63 | present->prev = temp; 64 | temp->next = present; 65 | } 66 | 67 | } 68 | 69 | void display(node** n){ 70 | node* present = (*n)->next; 71 | while(present != *n){ 72 | printf("%d ",present->data); 73 | present = present->next; 74 | } 75 | printf("%d\n",present->data); //To print data of *n 76 | } 77 | 78 | void delete_start(node** n){ 79 | if(*n == NULL){ 80 | printf("Empty List\n"); 81 | } 82 | else{ 83 | node* temp = (*n)->next; 84 | (*n)->next = temp->next; 85 | temp->next->prev = (*n); 86 | free(temp); 87 | } 88 | 89 | } 90 | 91 | void delete_end(node** n){ 92 | if(*n == NULL){ 93 | printf("Empty List\n"); 94 | } 95 | else{ 96 | node* temp = (*n); 97 | (*n)->prev->next = (*n)->next; 98 | (*n)->next->prev = (*n)->prev; 99 | (*n) = (*n)->prev; 100 | free(temp); 101 | } 102 | } 103 | 104 | void delete_pos(node** n, int pos){ 105 | if(*n == NULL){ 106 | printf("Empty List\n"); 107 | } 108 | else{ 109 | node* temp = (*n)->next; 110 | node* previous = (*n); 111 | for(int i=1; inext; 114 | } 115 | 116 | previous->next = temp->next; 117 | temp->next->prev = previous; 118 | if(temp == *n){ 119 | *n = previous; 120 | } 121 | free(temp); 122 | } 123 | } 124 | 125 | int main(){ 126 | node* last = NULL; 127 | insert_start(&last, 10); 128 | insert_start(&last, 20); 129 | insert_start(&last, 30); 130 | insert_start(&last, 40); 131 | insert_end(&last, 50); 132 | insert_end(&last, 60); 133 | insert_end(&last, 70); 134 | insert_end(&last, 80); 135 | insert_pos(&last, 100, 0); 136 | display(&last); 137 | delete_pos(&last, 10); 138 | display(&last); 139 | } -------------------------------------------------------------------------------- /DSA/Code/evaluate_postfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //--------------- EVALUATE POSTFIX 5 | 6 | int is_operator(char ch){ 7 | switch(ch){ 8 | case '+': 9 | case '-': 10 | case '/': 11 | case '*': 12 | case '$': return 1; 13 | default: return 0; 14 | } 15 | } 16 | 17 | void push(int* s, int* t, int r){ 18 | s[++(*t)] = r; 19 | } 20 | 21 | int pop(int* s, int* t){ 22 | return s[(*t)--]; 23 | } 24 | 25 | int eval(char* postfix){ 26 | int s[20], top=-1, i=0; 27 | int op1, op2; 28 | char ch; 29 | int a; 30 | 31 | while(postfix[i] != '\0'){ 32 | ch = postfix[i]; 33 | if(is_operator(ch)){ 34 | op2 = pop(s,&top); 35 | op1 = pop(s,&top); 36 | switch(ch){ 37 | case '+': push(s,&top,op1+op2); 38 | break; 39 | case '-': push(s,&top,op1-op2); 40 | break; 41 | case '*': push(s,&top,op1*op2); 42 | break; 43 | case '/': push(s,&top,op1/op2); 44 | break; 45 | /* 46 | case '$': push(s,&top,pow(op1,op2)); 47 | break; 48 | */ 49 | } 50 | } 51 | else{ 52 | 53 | printf("%c: ",ch); 54 | scanf("%d", &a); 55 | push(s,&top,a); 56 | } 57 | 58 | i++; 59 | } 60 | 61 | return(pop(s,&top)); 62 | } 63 | 64 | int main(){ 65 | char postfix[20]; 66 | printf("Enter the postfix expression: "); 67 | scanf("%s",postfix); 68 | printf("The result is: %d\n",eval(postfix)); 69 | return 0; 70 | } -------------------------------------------------------------------------------- /DSA/Code/expression_tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node{ 5 | char data; 6 | struct Node* left; 7 | struct Node* right; 8 | }node; 9 | 10 | typedef struct Tree{ 11 | node* root; 12 | }tree; 13 | 14 | typedef struct Stack{ 15 | node* s[50]; 16 | int top; 17 | }stack; 18 | 19 | void initialize_tree(tree* t){ 20 | t->root = NULL; 21 | } 22 | 23 | void initialize_stack(stack* s){ 24 | s->top = -1; 25 | } 26 | 27 | void push(stack* s, node* root){ 28 | if(s->top < 50){ 29 | s->s[++(s->top)] = root; 30 | } 31 | } 32 | 33 | node* pop(stack* s){ 34 | if(s->top != -1){ 35 | return s->s[(s->top)--]; 36 | } 37 | } 38 | 39 | int is_operator(char x){ 40 | switch(x){ 41 | case '+': 42 | case '-': 43 | case '*': 44 | case '/': 45 | return 1; 46 | default: 47 | return 0; 48 | } 49 | } 50 | 51 | void create(tree* t, stack* s, char* postfix){ 52 | node* temp; 53 | int i=0; 54 | 55 | while(postfix[i] != '\0'){ 56 | temp = (node*)malloc(sizeof(node)); 57 | temp->data = postfix[i]; 58 | temp->left = NULL; 59 | temp->right = NULL; 60 | 61 | if(is_operator(postfix[i])){ 62 | temp->right = pop(s); 63 | temp->left = pop(s); 64 | } 65 | 66 | push(s, temp); 67 | 68 | i++; 69 | } 70 | 71 | t->root = pop(s); 72 | } 73 | 74 | void in_order(node* root){ 75 | if(root!=NULL){ 76 | in_order(root->left); 77 | printf("%c\n", root->data); 78 | in_order(root->right); 79 | } 80 | } 81 | 82 | float evaluate(node* root){ 83 | switch(root->data){ 84 | case '+': 85 | return evaluate(root->left) + evaluate(root->right); 86 | case '-': 87 | return evaluate(root->left) - evaluate(root->right); 88 | case '*': 89 | return evaluate(root->left) * evaluate(root->right); 90 | case '/': 91 | return evaluate(root->left) / evaluate(root->right); 92 | default: 93 | return root->data - '0'; 94 | } 95 | } 96 | 97 | int main(){ 98 | tree t; 99 | stack s; 100 | char postfix[20] = "12+"; 101 | initialize_tree(&t); 102 | initialize_stack(&s); 103 | create(&t, &s, postfix); 104 | in_order(t.root); 105 | printf("%f\n", evaluate(t.root)); 106 | } -------------------------------------------------------------------------------- /DSA/Code/infix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //--------------- INFIX TO POSTFIX 5 | 6 | void push(char* s, int* t, char ch){ 7 | s[++(*t)] = ch; 8 | } 9 | 10 | char peek(char* s, int* t){ 11 | return s[*t]; 12 | } 13 | 14 | char pop(char* s, int* t){ 15 | return s[(*t)--]; 16 | } 17 | 18 | int stkprec(char ch){ 19 | switch(ch){ 20 | case '+': 21 | case '-': return 2; 22 | 23 | case '*': 24 | case '/': return 4; 25 | 26 | case '$': return 5; 27 | 28 | case '(': return 0; 29 | 30 | case '#': return -1; 31 | 32 | default: return 8; 33 | } 34 | } 35 | 36 | int ipprec(char ch){ 37 | switch(ch){ 38 | case '+': 39 | case '-': return 1; 40 | 41 | case '*': 42 | case '/': return 3; 43 | 44 | case '$': return 6; 45 | 46 | case '(': return 9; 47 | 48 | case ')': return 0; 49 | 50 | default: return 7; 51 | } 52 | } 53 | 54 | void infix_postfix(char* infix, char* postfix){ 55 | char s[20]; 56 | int top=-1, i=0, j=0; 57 | 58 | push(s,&top,'#'); 59 | 60 | while(infix[i]){ 61 | while(stkprec(peek(s,&top)) > ipprec(infix[i])){ 62 | postfix[j++] = pop(s,&top); 63 | } 64 | 65 | if(stkprec(peek(s,&top)) < ipprec(infix[i])){ 66 | push(s,&top,infix[i]); 67 | } 68 | else{ 69 | pop(s,&top); 70 | } 71 | 72 | i++; 73 | } 74 | 75 | while(peek(s,&top)!='#'){ 76 | postfix[j++]=pop(s,&top); 77 | postfix[j]='\0'; 78 | } 79 | 80 | } 81 | 82 | int main(){ 83 | char infix[20], postfix[20]; 84 | printf("Enter the infix expression\n"); 85 | scanf("%s",infix); 86 | infix_postfix(infix, postfix); 87 | printf("Postfix Expression:\n%s\n",postfix); 88 | } -------------------------------------------------------------------------------- /DSA/Code/josephus.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node{ 5 | int position; 6 | struct Node* link; 7 | }node; 8 | 9 | void insert(node** n, int x){ 10 | node* temp = (node*)malloc(sizeof(node)); 11 | temp->position = x; 12 | temp->link = temp; 13 | 14 | if(*n == NULL){ 15 | *n = temp; 16 | } 17 | else{ 18 | temp->link = (*n)->link; 19 | (*n)->link = temp; 20 | } 21 | } 22 | 23 | int josephus(node** n, int k){ 24 | node* present = (*n)->link; 25 | node* previous = *n; 26 | int i=1; 27 | while(present->link != present){ 28 | if(i%k == 0){ 29 | previous->link = present->link; 30 | free(present); 31 | present = previous->link; 32 | } 33 | else{ 34 | previous = present; 35 | present = present->link; 36 | } 37 | i++; 38 | } 39 | return present->position; 40 | } 41 | 42 | int main(){ 43 | node* head = NULL; 44 | int n=10, k=4; 45 | for(int i=n; i>0; i--){ 46 | insert(&head, i); 47 | } 48 | printf("%d\n",josephus(&head,k)); 49 | } -------------------------------------------------------------------------------- /DSA/Code/paranthesis_matching.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //--------------- USING ARRAYS 4 | char pop(char* s, int* t){ 5 | return(s[(*t)--]); 6 | } 7 | 8 | void push(char* s, int* t, char x){ 9 | s[++(*t)] = x; 10 | } 11 | 12 | int compare(char* s, int* t, char x){ 13 | char popped; 14 | if(*t == -1){ 15 | return 0; 16 | } 17 | else{ 18 | popped = pop(s,t); 19 | switch(x){ 20 | case ')': if(popped == '(') return 1; 21 | case '}': if(popped == '{') return 1; 22 | case ']': if(popped == '[') return 1; 23 | } 24 | return 0; 25 | } 26 | } 27 | 28 | int par_match(char* e){ 29 | char s[20]; 30 | int top=-1; 31 | int i=0; 32 | int match=1; 33 | 34 | while(e[i] != '\0'){ 35 | switch(e[i]){ 36 | case '(': 37 | case '[': 38 | case '{': 39 | push(s,&top,e[i]); 40 | break; 41 | case ')': 42 | case ']': 43 | case '}': 44 | match = compare(s,&top,e[i]); 45 | break; 46 | default: return -1; 47 | } 48 | if(!match){ 49 | return 0; 50 | } 51 | i++; 52 | } 53 | if(top != -1){ 54 | return 0; 55 | } 56 | else{ 57 | return 1; 58 | } 59 | } 60 | 61 | int main(){ 62 | char expression[20]; 63 | printf("Enter the expression: "); 64 | scanf("%s",expression); 65 | if(par_match(expression)){ 66 | printf("Paranthesis are matching\n"); 67 | } 68 | else{ 69 | printf("Paranthesis are not matching\n"); 70 | } 71 | } 72 | 73 | //--------------- USING LINKED LIST 74 | 75 | 76 | // Design a method for keeping two stacks within a single linear array so that neither stack overflows until all of the memory is used and an entire stack is never shifted to a different location within the array. Write C routines push1, push2, pop1, pop2 to manipulate two stacks. 77 | // Hint - the two stacks grow towards each other -------------------------------------------------------------------------------- /DSA/Code/priority_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct priority_queue{ 4 | int data; 5 | int priority; 6 | }queue; 7 | 8 | /* 9 | void insert(queue *pq, int* count, int x, int p){ 10 | int j = *count - 1; 11 | queue temp; 12 | temp.data = x; 13 | temp.priority = p; 14 | 15 | while(j>=0 && pq[j].priority > temp.priority){ 16 | pq[j+1] = pq[j]; 17 | j--; 18 | } 19 | 20 | pq[j+1] = temp; 21 | (*count)++; 22 | } 23 | 24 | queue delete(queue *pq, int* count){ 25 | queue temp; 26 | if(count == 0){ 27 | temp.data = 0; 28 | temp.priority = -1; 29 | } 30 | else{ 31 | temp = pq[0]; 32 | for(int j=1; j<*count; j++){ 33 | pq[j-1] = pq[j]; 34 | } 35 | (*count)--; 36 | } 37 | return temp; 38 | } 39 | */ 40 | 41 | void insert(queue *pq, int* count, int x, int p){ 42 | pq[++(*count)].data = x; 43 | pq[(*count)].priority = p; 44 | } 45 | 46 | void display(queue *pq, int count){ 47 | if(count > -1){ 48 | for(int i=0; i max){ 61 | max = pq[i].priority; 62 | } 63 | } 64 | 65 | // +1 to take care of deleting the highest priority node 66 | return max+1; 67 | } 68 | 69 | void delete(queue *pq, int* count){ 70 | if(*count == -1){ 71 | printf("Empty Queue\n"); 72 | } 73 | else{ 74 | queue temp; 75 | temp.priority = max_priority(pq, *count); 76 | int index = -1; 77 | 78 | for(int i=0; i<*count+1; i++){ 79 | if(pq[i].priority < temp.priority){ 80 | temp = pq[i]; 81 | index = i; 82 | } 83 | } 84 | 85 | for(int i=index; i<*count; i++){ 86 | pq[i] = pq[i+1]; 87 | } 88 | 89 | (*count)--; 90 | } 91 | } 92 | 93 | int main(){ 94 | queue q[20]; 95 | int count = -1; 96 | insert(q, &count, 10, 2); 97 | insert(q, &count, 20, 1); 98 | insert(q, &count, 30, 4); 99 | insert(q, &count, 40, 3); 100 | delete(q, &count); 101 | delete(q, &count); 102 | delete(q, &count); 103 | delete(q, &count); 104 | delete(q, &count); 105 | display(q, count); 106 | insert(q, &count, 20, 1); 107 | insert(q, &count, 30, 4); 108 | insert(q, &count, 40, 3); 109 | display(q, count); 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /DSA/Code/priority_queue_heap.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct Element{ 4 | int data; 5 | int priority; 6 | }element; 7 | 8 | void heapify(element* q, int i){ 9 | element temp; 10 | while(i > 0 && q[i].priority > q[(i-1)/2].priority){ 11 | temp = q[i]; 12 | q[i] = q[(i-1)/2]; 13 | q[(i-1)/2] = temp; 14 | i = (i-1)/2; 15 | } 16 | } 17 | 18 | void enqueue(element* q, int* count, int x, int priority){ 19 | q[++(*count)].data = x; 20 | q[(*count)].priority = priority; 21 | heapify(q, *count); 22 | } 23 | 24 | void display(element* q, int count){ 25 | for(int i=0; i<=count; i++){ 26 | printf("%d\n", q[i].data); 27 | } 28 | printf("\n"); 29 | } 30 | 31 | void dequeue(element* q, int* count){ 32 | q[0] = q[(*count)--]; 33 | for(int i=1; i<=*count; i++){ 34 | heapify(q, i); 35 | } 36 | } 37 | 38 | int main(){ 39 | element queue[100]; 40 | int count = -1; 41 | enqueue(queue, &count, 10, 4); 42 | enqueue(queue, &count, 20, 2); 43 | enqueue(queue, &count, 30, 1); 44 | enqueue(queue, &count, 40, 3); 45 | enqueue(queue, &count, 50, 5); 46 | display(queue,count); 47 | dequeue(queue, &count); 48 | display(queue,count); 49 | dequeue(queue, &count); 50 | display(queue,count); 51 | } -------------------------------------------------------------------------------- /DSA/Code/queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void enqueue(int* q, int* f, int* r, int size, int x){ 5 | if(*r == size-1){ 6 | printf("Queue Overflow\n"); 7 | } 8 | else{ 9 | q[++(*r)] = x; 10 | } 11 | if(*f == -1){ 12 | *f=0; 13 | } 14 | } 15 | 16 | void display(int* q, int* f, int* r){ 17 | if(*f == -1){ 18 | printf("Queue Underflow\n"); 19 | } 20 | for(int i=*f; i <= *r; i++){ 21 | printf("%d ",q[i]); 22 | } 23 | printf("\n"); 24 | } 25 | 26 | int dequeue(int* q, int* f, int* r){ 27 | if(*f == -1){ 28 | printf("Queue Underflow\n"); 29 | return -1; 30 | } 31 | else{ 32 | int x = q[*f]; 33 | if(*f == *r){ 34 | f = r = -1; 35 | } 36 | else{ 37 | (*f++); 38 | } 39 | return x; 40 | } 41 | } 42 | 43 | int main(){ 44 | int f=-1, r=-1; 45 | int size = 4; 46 | int* q = (int*)malloc(sizeof(int)); 47 | 48 | enqueue(q,&f,&r,size,10); 49 | enqueue(q,&f,&r,size,20); 50 | enqueue(q,&f,&r,size,30); 51 | enqueue(q,&f,&r,size,40); 52 | display(q,&f,&r); 53 | } -------------------------------------------------------------------------------- /DSA/Code/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void push(int* s, int* t, int size, int x){ 5 | if(*t == size-1){ 6 | printf("Stack Overflow\n"); 7 | } 8 | else{ 9 | (*t)++; 10 | s[*t] = x; 11 | } 12 | } 13 | 14 | void display(int* s, int* t){ 15 | if(*t == -1){ 16 | printf("Stack Underflow"); 17 | } 18 | else{ 19 | for(int i=*t; i>-1; i--){ 20 | printf("%d ",s[i]); 21 | } 22 | } 23 | printf("\n"); 24 | } 25 | 26 | void pop(int* s, int* t){ 27 | if(*t == -1){ 28 | printf("Stack Underflow\n"); 29 | } 30 | else{ 31 | (*t)--; 32 | } 33 | } 34 | 35 | int main(){ 36 | int* s; 37 | int top, size, data; 38 | 39 | size = 4; 40 | s = (int*)malloc(sizeof(int)); 41 | top = -1; 42 | 43 | } -------------------------------------------------------------------------------- /DSA/D0-Tree.md: -------------------------------------------------------------------------------- 1 | 2 | # TREE 3 | --- 4 | - Non-linear and heirarchical data structure consisting of a collection of nodes such that each node stores a value and a set of references to the other nodes 5 | ![tree.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/tree.png) 6 | 7 | - Data is not in a sequential manner 8 | 9 | | Terminology | Meaning | 10 | | ------------- | -------------------------------------------------------- | 11 | | Parent node | The predecessor node | 12 | | Child node | The immediate successor node | 13 | | Root node | Topmost node of a tree which does not have a parent node | 14 | | Leaf node | Node which does not have a child node | 15 | | Ancestor | Any predecessor node on the path to the root node | 16 | | Descendant | Any node on the path towards the leaf node | 17 | | Sibling | Nodes of the same parent node | 18 | | Internal node | A node with atleast one child | 19 | | Subtree | Any node of the tree along with its descendants | 20 | 21 | ### Properties: 22 | --- 23 | - If a tree has `n` nodes, it will have `n-1`edges. There is only one path from one node to another 24 | - Depth of a node is the number of edges in the path from the root node to that particular node 25 | - Height of a node is the length of the longest path from that node to a leaf node 26 | - Height of a tree is the length of the longest path from the root node to a leaf node 27 | - The total number of subtrees attached to a node is the degree of the node. The degree of a leaf node must be 0, and the degree of the tree is maximum degree of a node 28 | - Traversing is done by breadth-first and depth-first 29 | 30 | ### Implementation: 31 | ---- 32 | ``` 33 | struct Node 34 | { 35 | int data; 36 | struct Node *left_child; 37 | struct Node *right_child; 38 | }; 39 | ``` 40 | 41 | ### ADT for Tree: 42 | ---- 43 | 44 | | Function | Function Name | 45 | | -------------------------- | ------------- | 46 | | Create a tree | create | 47 | | Insert data in a tree | insert | 48 | | Search for a specific data | search | 49 | 50 | There are 3 types of traversal in a tree- 51 | 1. Pre-order traversal 52 | - root -> left -> right 53 | 2. Post-order traversal 54 | - left-> right-> root 55 | 3. In-order traversal 56 | - left-> root -> right 57 | - In-orded for a binary search tree always gives data in ascending order 58 | 59 | ### Types: 60 | --- 61 | 1. General tree 62 | - No restriction on the number of nodes 63 | 2. Binary tree [[D1-Binary_Tree]] 64 | - A node can have maximum of two child nodes 65 | 3. Balanced tree 66 | - Heigth of the left subtree and right subtree are equal or differ by 1 67 | - Traversal is O(log n) 68 | 69 | ### Applications: 70 | ---- 71 | - Spanning trees which are the shortest path used in routers to direct packets 72 | - Binary search tree which is used in searching and sorting algorithms 73 | - Storing hierarchical data 74 | - Synatx tree represents structure of a program and is sued in compilers 75 | - Trie(prefix tree) is fast and efficient for dynamic spell checking 76 | - Heap is also a tree data structure, used to implement priority queues 77 | -------------------------------------------------------------------------------- /DSA/D1-Binary_Tree.md: -------------------------------------------------------------------------------- 1 | # BINARY TREE 2 | --- 3 | - Nodes have maximum of 2 child nodes 4 | - A child node cannot have different parents 5 | 6 | ### Types: 7 | --- 8 | 1. Full/Strictly Binary Tree 9 | - Every node except leaf nodes have 2 child nodes 10 | 2. Perfect Binary Tree 11 | - All leaf nodes are at the same level 12 | - If the depth is `d`, there are `0 to d` levels and `2^(d+1) - 1` total number of nodes 13 | - The number of leaf nodes is 1 + the number of internal nodes 14 | 3. Complete Binary Tree 15 | - Every level must be completely filled 16 | - All leaf elements must lean to the left 17 | - The last leaf element might not have a right sibling 18 | - [Binary Heap](https://www.geeksforgeeks.org/binary-heap/) is a practical example 19 | 4. Degenerate Tree 20 | - Every internal node has 1 child 21 | - Equivalent to linked lists 22 | - Traversal is O(n) 23 | 5. Skewed Tree 24 | - Degenerate tree with either left nodes or right nodes 25 | 6. [[D3-Expression_Tree | Expression Tree]] 26 | 27 | 28 | ## Binary Search Tree 29 | ---- 30 | - Binary tree that is either empty or contains nodes which satisfies the following conditions 31 | - The key in the left child node is lesser than the key in the parent node 32 | - The key in the right child node is greater than the key in the parent node 33 | - The right and left subtrees are also binary search trees 34 | 35 | ### Implementation: 36 | ---- 37 | #### 1. Array Implementation: 38 | ``` 39 | int t[100]; 40 | for(int i=0; i<100; i++){ 41 | t[i] = -1; 42 | } 43 | ``` 44 | -> unused nodes are set to -1 45 | 46 | ##### 1. Insertion 47 | ``` 48 | void insert(int* t, int x){ 49 | int i=0; 50 | while(t[i] != -1){ 51 | if(t[i]>x){ 52 | i = i*2 + 1; 53 | } 54 | else{ 55 | i = i*2 + 2; 56 | } 57 | } 58 | t[i] = x; 59 | ``` 60 | 61 | ##### 2. Traversal 62 | ``` 63 | void preorder(int* t, int i){ 64 | if(t[i]!=-1){ 65 | printf("%d ",t[i]); 66 | preorder(t, i*2 + 1); 67 | preorder(t, i*2 + 2); 68 | } 69 | } 70 | ``` 71 | 72 | ``` 73 | void inorder(int* t, int i){ 74 | if(t[i]!=-1){ 75 | inorder(t, i*2 + 1); 76 | printf("%d ",t[i]); 77 | inorder(t, i*2 + 2); 78 | } 79 | } 80 | ``` 81 | 82 | ``` 83 | void postorder(int* t, int i){ 84 | if(t[i]!=-1){ 85 | postorder(t, i*2 + 1); 86 | postorder(t, i*2 + 2); 87 | printf("%d ",t[i]); 88 | } 89 | } 90 | ``` 91 | 92 | #### 2. [[D2-Linked_List_Implementation || Linked List Implementation]] 93 | 94 | -------------------------------------------------------------------------------- /DSA/D2-Linked_List_Implementation.md: -------------------------------------------------------------------------------- 1 | # LINKED LIST IMPLEMENTATION 2 | ---- 3 | 4 | ``` 5 | typedef struct TreeNode{ 6 | int data; 7 | struct TreeNode* left; 8 | struct TreeNode* right; 9 | }node; 10 | ``` 11 | 12 | ``` 13 | typedef struct Tree{ 14 | node* root; 15 | }tree; 16 | ``` 17 | 18 | ##### 1. Creation 19 | ``` 20 | void create_tree(tree* t){ 21 | t->root = NULL; 22 | } 23 | ``` 24 | 25 | ##### 2. Insertion 26 | ``` 27 | void insert(tree* t, int x){ 28 | node* temp = (node*)malloc(sizeof(node)); 29 | temp->data = x; 30 | temp->right = NULL; 31 | temp->left = NULL; 32 | 33 | if(t->root == NULL){ 34 | t->root = temp; 35 | } 36 | else{ 37 | node* root = t->root; 38 | while(1){ 39 | if(root->data > temp->data){ 40 | if(root->left == NULL){ 41 | root->left = temp; 42 | break; 43 | } 44 | else{ 45 | root = root->left; 46 | } 47 | } 48 | else{ 49 | if(root->right == NULL){ 50 | root->right = temp; 51 | break; 52 | } 53 | else{ 54 | root = root->right; 55 | } 56 | } 57 | } 58 | } 59 | } 60 | ``` 61 | 62 | #### 3. Traversal 63 | ``` 64 | void pre_order(node* n){ 65 | if(n!=NULL){ 66 | printf("%d\n", n->data); 67 | pre_order(n->left); 68 | pre_order(n->right); 69 | } 70 | } 71 | ``` 72 | 73 | ``` 74 | void in_order(node* n){ 75 | if(n!=NULL){ 76 | in_order(n->left); 77 | printf("%d\n", n->data); 78 | in_order(n->right); 79 | } 80 | } 81 | ``` 82 | 83 | ``` 84 | void post_order(node* n){ 85 | if(n!=NULL){ 86 | post_order(n->left); 87 | post_order(n->right); 88 | printf("%d\n", n->data); 89 | } 90 | } 91 | ``` 92 | 93 | #### 4. Deletion 94 | - There are 3 cases which arises while deleteing a node 95 | 1. Node with no child 96 | - Set corresponding field to NULL in the parent node 97 | 2. Node with 1 child 98 | - Connect the child to the parent of the node 99 | 3. Node with 2 child 100 | - Replace the node to be deleted with either its inorder successor or its inorder predecessor 101 | - Delete the predecessor/successor node according to the first 2 cases 102 | - The successor is used for the implementation below 103 | 104 | ``` 105 | node* delete(node* root, int x){ 106 | if(root!=NULL){ 107 | if(root->data > x){ 108 | root->left = delete(root->left, x); 109 | return root; 110 | } 111 | else if(root->data < x){ 112 | root->right = delete(root->right, x); 113 | return root; 114 | } 115 | 116 | if(root->left == NULL){ 117 | node* temp = root->right; 118 | free(root); 119 | return temp; 120 | } 121 | else if(root->right == NULL){ 122 | node* temp = root->left; 123 | free(root); 124 | return temp; 125 | } 126 | 127 | node* successor = root->right; 128 | node* successor_root = root; 129 | while(successor->left != NULL){ 130 | successor_root = successor; 131 | successor = successor->left; 132 | } 133 | 134 | if(successor_root != root){ 135 | successor_root->left = successor->right; 136 | } 137 | else{ 138 | successor_root->right = successor->right; 139 | } 140 | 141 | root->data = successor->data; 142 | free(successor); 143 | return root; 144 | } 145 | } 146 | ``` -------------------------------------------------------------------------------- /DSA/D5-Heap.md: -------------------------------------------------------------------------------- 1 | # HEAP 2 | ---- 3 | - Complete binary tree 4 | - 2 types 5 | 1. Max Heap - descending tree 6 | 2. Min Heap - ascending tree 7 | 8 | - 2 approaches to construct a binary heap 9 | 1. Top Down Approach 10 | 2. Bottom Up Approach 11 | 12 | - Typically represented by arrays 13 | ``` 14 | Arr[(i-1)/2] Returns the parent node 15 | Arr[(2*i)+1] Returns the left child node 16 | Arr[(2*i)+2] Returns the right child node 17 | ``` 18 | 19 | ### Implementation: 20 | 21 | -> Heapify operation - Converts a binary tree into a heap 22 | ``` 23 | void heapify(int* h, int n){ 24 | int i, j, k, key; 25 | for(k=1; k0 && key>h[j]){ 30 | h[i] = h[j]; 31 | i = j; 32 | j=(i-1)/2; 33 | } 34 | h[i] = key; 35 | } 36 | } 37 | ``` 38 | 39 | 40 | ### Deletion of a node: 41 | - The standard delete operation is to delete the element present at the root node of the heap 42 | - Deleting an element at any intermediary position in a heap can be costly 43 | - So replace the element to be deleted by the last node and delete the last node of the heap 44 | - Perform `heapify` operation to ensure it remains as a max heap or min heap 45 | 46 | ### Applications: 47 | 1. Heap Sort - sort an array in O(n log n) time 48 | 2. [[D6-Priority_Queue_Heap|Priority Queue]] - supports insert, delete extractmax and decreasekey methods in O(log n) time 49 | 3. Graph algorithms like Dijkstra's shortest path and Prim's minimum spanning trees -------------------------------------------------------------------------------- /DSA/D6-Priority_Queue_Heap.md: -------------------------------------------------------------------------------- 1 | # PRIORITY QUEUE 2 | --- 3 | - Heap allows an efficient implementation of a priority queue - O(log n) for most operations 4 | - This page contains only the implementation using a heap, for more details about priority queue please visit [[C5-Priority_Queue]] 5 | 6 | ``` 7 | void heapify(element* q, int i){ 8 | element temp; 9 | while(i > 0 && q[i].priority > q[(i-1)/2].priority){ 10 | temp = q[i]; 11 | q[i] = q[(i-1)/2]; 12 | q[(i-1)/2] = temp; 13 | i = (i-1)/2; 14 | } 15 | } 16 | 17 | void enqueue(element* q, int* count, int x, int priority){ 18 | q[++(*count)].data = x; 19 | q[(*count)].priority = priority; 20 | heapify(q, *count); 21 | } 22 | 23 | void display(element* q, int count){ 24 | for(int i=0; i<=count; i++){ 25 | printf("%d\n", q[i].data); 26 | } 27 | printf("\n"); 28 | } 29 | 30 | void dequeue(element* q, int* count){ 31 | q[0] = q[(*count)--]; 32 | for(int i=1; i<=*count; i++){ 33 | heapify(q, i); 34 | } 35 | } 36 | ``` -------------------------------------------------------------------------------- /DSA/D7-N_ary_Tree.md: -------------------------------------------------------------------------------- 1 | # N-ary Tree 2 | --- 3 | - A rooted tree in which each node has a maximum of n children 4 | - Either the maximum number of children nodes are fixed, 5 | ``` 6 | typedef struct Node{ 7 | int data; 8 | struct Node* child[MAX]; 9 | }node; 10 | ``` 11 | Or only the first(oldest) node is linked to the parent node, and each node has a link to its sibling node 12 | ``` 13 | typedef struct Node{ 14 | int data; 15 | struct Node* child; 16 | struct Node* sibling; 17 | }node; 18 | ``` 19 | 20 | ## Converting N-ary to Binary 21 | 22 | #### 1. Left child - right sibling 23 | - Link all the siblings of a node 24 | - Remove all the links of a node to its children, except for the leftmost child 25 | - The resulting binary tree will always have an empty right subtree for the root node as the root node does not have any siblings 26 | 27 | #### 2. Forest 28 | - A forest is a collection of binary trees 29 | - Convert each tree into a binary tree by the above method 30 | - Link all the binary trees together through the right child of the root node 31 | 32 | 33 | ## Traversal 34 | 35 | #### 1. Preorder 36 | ``` 37 | void preorder(node *root) 38 | { 39 | if(root!=NULL) 40 | { 41 | printf("%d\n",root->info); 42 | preorder(root->child); 43 | preorder(root->sibling); 44 | } 45 | } 46 | ``` 47 | 48 | #### 2. Postorder 49 | ``` 50 | void postorder(node *root) 51 | { 52 | if(root!=NULL) 53 | { 54 | postorder(root->child); 55 | postorder(root->sibling); 56 | printf("%d\n", root->info); 57 | } 58 | } 59 | ``` 60 | 61 | #### 3. Inorder 62 | ``` 63 | void inorder(node *root) 64 | { 65 | if(root!=NULL) 66 | { 67 | inorder(root->child); 68 | printf("%d\n",root->info); 69 | inorder(root->sibling); 70 | } 71 | } 72 | ``` -------------------------------------------------------------------------------- /DSA/D8A-AVL_Tree.md: -------------------------------------------------------------------------------- 1 | # AVL TREE 2 | --- 3 | - Self balancing binary search tree where the difference of heights of the left and the right subtrees cannot be more than one for all nodes 4 | - The height of a `n` node avl tree is always `log(n)` 5 | 6 | > Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take **O(h)** time where **h** is the height of the BST. The cost of these operations may become **O(n)** for a **skewed Binary tree**. If we make sure that the height of the tree remains **O(log(n))** after every insertion and deletion, then we can guarantee an upper bound of **O(log(n))** for all these operations. 7 | 8 | ## Insertion 9 | - To maintain the property of AVL trees after every insertion, the tree is rebalanced by performing 10 | 1. Left rotation: 11 | - ![left_rotation.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/left_rotation.png) 12 | 2. Right rotation 13 | - ![right_rotation.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/right_rotation.png) 14 | - The tree can be rotated any number of times 15 | 16 | - Common combinations of rotations: 17 | 1. Left-Right rotation 18 | - ![lr1.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/lr1.png) 19 | - ![[lr2.png]](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/lr2.png) 20 | 2. Right-Left rotation 21 | - ![rl1.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/rl1.png) 22 | - ![[rl2.png]](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/rl2.png) 23 | 24 | ### Algorithm 25 | 1. Insert an element as you would for a binary search tree 26 | 2. Check the balance factor(difference of nodes in left and right sub tree) 27 | 3. If the balance factor of every node is less than or equal to 1, proceed 28 | 4. Else perform suitable rotation operations 29 | 30 | ## Deletion 31 | - Deleting a node might cause an imbalance in the height of the tree 32 | 33 | ### Algorithm 34 | 1. Delete an element as you would for a binary search tree 35 | 2. Check the balance factor 36 | 3. If the balance factor of every node is less than or equal to 1, proceed 37 | 4. Else, if `balance factor > 1` refer the **left subtree** . Otherwise refer the **right subtree** 38 | 39 | | Balance Factor of Tree | Balance Factor of Subtree | Operation | 40 | | ---------------------- | -------------------------- | --------------------------------------------------------------------------------------- | 41 | | greater than 1 | greater than 0 | left-left case: perform right rotation | 42 | | | equal to 0 | left-left case: perform right rotaion **OR** left-right case: perform left-right rotation | 43 | | lesser than -1 | lesser than 0 | right-right case: perform left rotation | 44 | | | equal to 0 | right-right case: perform left rotation **OR** right-left case: perform right-left rotation | 45 | 46 | -------------------------------------------------------------------------------- /DSA/D8C-B_Tree.md: -------------------------------------------------------------------------------- 1 | # BAYER'S TREE 2 | --- 3 | - Special tree where a node can have more than one key and any number of children 4 | - Stores data in a sorted order 5 | - Also called as B Tree , Balanced M-way Search Tree and K way tree 6 | 7 | ## Properties 8 | - A tree of order `n` can have a maximum of `n` children 9 | - Every node except root node should contain a minimum of `(n/2) - 1` and a maximum `n-1` keys 10 | - Root node should have a minimum of 1 key 11 | - All the non leaf nodes must have `(n/2)` keys 12 | - All the keys must be in sorted increasing order 13 | - The tree grows and shrinks from the leaf node 14 | - If the root is a non leaf, then it must have atleast two children 15 | - Minimum height of a B tree with `N` nodes is `log{base n}(N+1) -1` 16 | - 17 | ## Advantages 18 | - Reduces number of reads made on a disk 19 | - Can be easily optimized according to the disk size 20 | - Specially designed to handle bulky amout of data 21 | - Useful algorithm for databases and file systems 22 | 23 | ## Insertion 24 | ### Algorithm 25 | 1. Travers the B-Tree to find the appropriate leaf node 26 | 2. If the tree contains `n-1` keys, insert the element in increasing order 27 | 3. Else, 28 | 1. Insert the new element in increasing order 29 | 2. Split the node into two at the median 30 | 3. Push the median element to the parent node 31 | 4. Repeat the above procedure if the parent node also contains `n-1` keys 32 | 33 | -------------------------------------------------------------------------------- /DSA/E0-Graph.md: -------------------------------------------------------------------------------- 1 | # Graph 2 | ---- 3 | - Non linear data structure where elements are represented as a node or vertex 4 | - Nodes are connected with edges or arcs 5 | - A graph is represented by G(V,E) -> an ordered pair of vertices and edges 6 | 7 | | Tree | Graph | 8 | | ------------------------------------------ | --------------------------------------------------- | 9 | | Has a root node | No root node | 10 | | Traversal: preorder, inorder and postorder | Traversal: breadth first search, depth first search | 11 | | Acyclic, hierarchical | Can be either cyclic or acyclic | 12 | | Directed: root node to leaf | Can be either directed or undirected | 13 | 14 | ### Applications 15 | 1. Computer networks: [[E4-Network_Topologies]] 16 | 2. Finding shortest path 17 | 3. Social networks 18 | 4. Neural networks 19 | 5. Artificial Intelligence 20 | 6. Pathfinding algorithms 21 | 7. Resource allocation 22 | 23 | ### Types 24 | 1. Directed graph (Digraph) and Undirected graph 25 | 2. Weighted graph and Unweighted graph 26 | 3. Cyclic graph: a graph with atleast one path where the source and destination nodes are the same; Acyclic graph: a grahp with no cycles, for example a tree 27 | 4. Connected graph and Disconnected graph 28 | 29 | ### Terminologies 30 | | Name | Definition | 31 | | ------------- | -------------------------------------------------------------------------- | 32 | | Vertex | A node which holds the data element | 33 | | Edge | Path between a pair of vertices | 34 | | Adjacency | Two nodes are adjacent to each other if they are connected through an edge | 35 | | Path | Sequence of edges between the nodes | 36 | | Weighted edge | An edge which has a cost or a value associated with it | 37 | | Degree | Total number of edges connected to a vertex | 38 | | In-degree | Total number of incomming edges connected to a node | 39 | | Out-degree | Total number of outgoing edges connected to a node | 40 | | Self loop | A node which points to itself with a directed edge | 41 | 42 | ## Properties 43 | 1. Undirected graph 44 | - The number of possible pairs in a `m` vertex graph is `m*(m-1)` 45 | - The number of edges is `m*(m-1)/2` 46 | 2. Directed graph 47 | - The number of possible pairs in a `m` vertext graph is `m*(m-1)` 48 | - The number of edges is less than or equal to`m*(m-1)` 49 | 50 | ## Representation 51 | 1. Adjacency matrix (2D array) 52 | - Mark 1 if there is a path between the two nodes, else 0 53 | - For a weighted graph, mark the weight instead of 1 54 | - The matrix is symmetric for an undirected graph and may be asymmetric for a directed graph 55 | 2. Adjacency List 56 | - A linked list of all the nodes 57 | - Each node has a list of its adjacent nodes 58 | 59 | ## [[E1-Implementation || Implementation]] 60 | 61 | ## [[E2-Traversal | Traversal]] 62 | 63 | ## [[E3-Connectivity | Connectivity of Graphs]] 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /DSA/E1-Implementation.md: -------------------------------------------------------------------------------- 1 | # IMPLEMENTATION OF GRAPH 2 | --- 3 | ## Using Adjacency Matrix 4 | 1. For a graph of `N` nodes, create a 2D array of dimensions `NxN` and initialize all values to 0 5 | 2. For each edge in the `arr[X][Y]`, update `adj[X][Y]` and `adj[Y][X]` to 1 which will denote that there is a edge between `X` and `Y` 6 | 3. If it is a directed graph, update only `adj[X][Y]` to 1. If it is a weighted graph, update `adj[X][Y]` to the weight of the edge 7 | 8 | ``` 9 | #include 10 | 11 | // N nodes and M edges 12 | int N, M; 13 | 14 | void adjacency_matrix(int adj[][N], int arr[][2]){ 15 | for(int i=0; i=0 && y=0){ 55 | adj[x][y] = 1; 56 | adj[y][x] = 1; 57 | } 58 | } 59 | 60 | void remove_edge(int adj[][N], int x, int y){ 61 | if(x=0 && y=0){ 62 | adj[x][y] = 0; 63 | adj[y][x] = 0; 64 | } 65 | } 66 | ``` 67 | 68 | ## Using Adjacency List 69 | ``` 70 | // N vertices and M edges 71 | int N, M; 72 | 73 | typedef struct Node{ 74 | int data; 75 | struct Node* link; 76 | }node; 77 | 78 | void insert_node(node** graph, int x, int y){ 79 | // x is current node and y is the adjacent node 80 | node* current = graph[x]; 81 | node* temp = (node*)malloc(sizeof(node)); 82 | temp->data = y; 83 | temp->link = NULL; 84 | 85 | 86 | if(current == NULL){ 87 | graph[x] = temp; 88 | } 89 | else{ 90 | while(current->link != NULL){ 91 | current = current->link; 92 | } 93 | current->link = temp; 94 | } 95 | } 96 | 97 | void adjacency_list(node** graph, int arr[][2]){ 98 | for(int i=0; idata); 116 | current = current->link; 117 | } 118 | 119 | printf("\n"); 120 | } 121 | } 122 | 123 | int main(){ 124 | N = 5; 125 | int arr[][2] = {{1,2}, {1,3}, {2,1}, {2,4}, {3,1}, {3,4}, {4,2}, {4,3}}; 126 | M = sizeof(arr)/sizeof(arr[0]); 127 | 128 | node* graph[N]; 129 | 130 | adjacency_list(graph, arr); 131 | display(graph); 132 | } 133 | ``` -------------------------------------------------------------------------------- /DSA/E3-Connectivity.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/E3-Connectivity.md -------------------------------------------------------------------------------- /DSA/E4-Network_Topologies.md: -------------------------------------------------------------------------------- 1 | # TOPOLOGIES 2 | ---- 3 | - Graphs are mainly used in telecommunication and computer networks 4 | - Networking uses the notation `G(N,L)` where `N` is the set of nodes and `L` is the set of links 5 | - Topology is the orded in which nodes and edges are arranged in a network 6 | 7 | ## 1. Ring Topology 8 | - Also called cycle graph 9 | - Each vertex has a degree of 2 and the vertices are connected such that the graph forms a circle 10 | ![ring1.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/ring1.png) 11 | ![ring2.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/ring2.png) 12 | ![ring3.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/ring3.png) 13 | 14 | ## 2. Star Topology 15 | - Consists of a central vertex which has links to other vertices 16 | - The central vertex has a degree `n-1` where `n` is the total number of nodes. Other vertices have a degree of 1 17 | ![star1.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/star1.png) 18 | ![star2.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/start2.png) 19 | 20 | ## 3. Mesh Topology 21 | - A complete graph where each node is connected to all the other nodes 22 | - Each node has a degree `n-1` where `n` is the total number of nodes 23 | ![mesh1.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/mesh1.png) 24 | ![mesh2.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/mesh2.png) 25 | 26 | ## 4. Bus Topology 27 | - A graph where starting and ending node have a degree of 1 and other nodes have a degree of 2 such that the graph forms a line 28 | 29 | ## 5. Hybrid Topology 30 | - Combination of two or more topologies 31 | ![hybrid.png](https://github.com/Shogunkayo/PES_Notes/blob/main/DSA/Images/hybrid.png) 32 | 33 | 34 | -------------------------------------------------------------------------------- /DSA/F0-Hashing.md: -------------------------------------------------------------------------------- 1 | # HASHING 2 | --- 3 | - Process of maping keys and values into a hash table using a hashing function 4 | - Usually represented by a smaller key, such that the original key is easier to find 5 | - A hash function is a function which maps key value into a hash table range. It usually involves folding, truncation and modulo operations 6 | 7 | ## Collision 8 | - Since a hash function gets us a small number for a key which is a big integer or string, there is a possibility that two keys result in the same value 9 | - Handled using some collision resolution technique 10 | 11 | ### 1. Open Addressing (Closed Hashing) 12 | - Handles collisions by storing all data in the hash table 13 | - Seeks out availability in the next spot created by the algorithm 14 | - The size of the table must be greater than the total number of keys 15 | - [[F2-Linear_Probing]] 16 | - [[F3-Double_Hashing]] 17 | 18 | ### 2. Seperate Chaining(Open Hashing) 19 | - Handles collisions by making every hash table cell point to linked lists of records with identical hash function values 20 | - [[F1-Seperate_Chaining]] 21 | 22 | ### 3. Rehashing 23 | - Increases the table size when the load factor becomes more than a predefined value 24 | `Load Factor = No of filled records / Total capacity` 25 | - Hashes all the keys again and stores it in the new table -------------------------------------------------------------------------------- /DSA/F1-Seperate_Chaining.md: -------------------------------------------------------------------------------- 1 | # SEPERATE CHAINING 2 | --- 3 | - A linked list is used to implement this technique 4 | - When multiple elements are hashed into the same index, the elements are inserted into a singly linked list to form a chain 5 | 6 | ![[seperate_chaining.png]] 7 | 8 | ### Advantages 9 | - Simple to implement 10 | - Hash table never fills up 11 | - Less sensitive to the hash function 12 | - Usually used when the number of elements to be inserted is not known 13 | 14 | ### Disadvantages 15 | - Cache performance is not good compared to open addressing 16 | - Wastage of space as some parts of the table are never used 17 | - If the chain becomes long, search time can become linear in the worst case 18 | - Extra space is needed for the links 19 | -------------------------------------------------------------------------------- /DSA/F2-Linear_Probing.md: -------------------------------------------------------------------------------- 1 | # LINEAR PROBING 2 | --- 3 | - The hash table is searched sequentially that starts from the original location of the hash. If in case the location that we get is already occupied, we check for the next available location. 4 | `hash(key) = (hash(key) + i) % table_size` where `i = 0,1,2,3....` 5 | -------------------------------------------------------------------------------- /DSA/F3-Double_Hashing.md: -------------------------------------------------------------------------------- 1 | # DOUBLE HASHING 2 | --- 3 | - Double hashing uses the idea of applying a second hash function to key when a collision occurs 4 | 5 | `hash(new) = (hash1(key) + i * hash2(key)) % table_size` 6 | 7 | - The first hash function is typically `hash1(key) = key % table_size` 8 | - The second hash function is usually `hash2(key) = prime - (key % prime)` where `prime` is a prime number smaller than the table size 9 | - The second hash function must ensure that 10 | - It must never evaluate to zero 11 | - Make sure all the cells can be probed 12 | 13 | ### Advantages 14 | - Produces a uniform distribution of records throughout a hash table 15 | - Does not yield any clusters 16 | - Effective method for resolving collisions 17 | 18 | ### Implementation 19 | -------------------------------------------------------------------------------- /DSA/G0-Trie.md: -------------------------------------------------------------------------------- 1 | # TRIE TREE 2 | --- 3 | Trie tree is a digital search tree where each node can contain `m` pointers. It is generally used to store strings 4 | - If used to store numbers, a key can have 10 pointers (digits 0 to 9) 5 | - If used to store words, a key can have 26 pointers 6 | - Trie is derived from reTRIEval 7 | - Can perform storing and retrieval faster than a hash table 8 | - Can be used for prefix based searching 9 | 10 | ### Advantages 11 | - We can efficiently do **prefix search** (or auto-complete) with Trie. 12 | - We can easily print all words in alphabetical order which is not easily possible with hashing. 13 | - There is no overhead of Hash functions in a Trie data structure. 14 | - Searching for a String even in the large collection of strings in a Trie data structure can be done in **O(L)** Time complexity, Where L is the number of words in the query string. This searching time could be even less than O(L) if the query string does not exist in the trie. 15 | 16 | ### Properties 17 | - There is one root node in each Trie. 18 | - Each node of a Trie represents a string and each edge represents a character. 19 | - Every node consists of hashmaps or **an array of pointers**, with each index representing a character and a flag to indicate if any string ends at the current node. 20 | - Trie data structure can contain any number of characters including **alphabets**, **numbers**, and **special characters**. For a lowercase alphabet set, the **0th** index represents **‘a’** and the **25th** index represents **‘z’** characters. 21 | - Each path from the root to any node represents a word or string. 22 | 23 | 24 | -------------------------------------------------------------------------------- /DSA/Images/dfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/dfs.png -------------------------------------------------------------------------------- /DSA/Images/hybrid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/hybrid.png -------------------------------------------------------------------------------- /DSA/Images/left_rotation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/left_rotation.png -------------------------------------------------------------------------------- /DSA/Images/lr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/lr1.png -------------------------------------------------------------------------------- /DSA/Images/lr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/lr2.png -------------------------------------------------------------------------------- /DSA/Images/mesh1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/mesh1.png -------------------------------------------------------------------------------- /DSA/Images/mesh2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/mesh2.png -------------------------------------------------------------------------------- /DSA/Images/multilist1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/multilist1.jpg -------------------------------------------------------------------------------- /DSA/Images/multilist2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/multilist2.jpg -------------------------------------------------------------------------------- /DSA/Images/right_rotation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/right_rotation.png -------------------------------------------------------------------------------- /DSA/Images/ring1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/ring1.png -------------------------------------------------------------------------------- /DSA/Images/ring2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/ring2.png -------------------------------------------------------------------------------- /DSA/Images/ring3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/ring3.png -------------------------------------------------------------------------------- /DSA/Images/rl1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/rl1.png -------------------------------------------------------------------------------- /DSA/Images/rl2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/rl2.png -------------------------------------------------------------------------------- /DSA/Images/sparse_matrix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/sparse_matrix.png -------------------------------------------------------------------------------- /DSA/Images/star1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/star1.png -------------------------------------------------------------------------------- /DSA/Images/start2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/start2.png -------------------------------------------------------------------------------- /DSA/Images/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/DSA/Images/tree.png -------------------------------------------------------------------------------- /Design and Analysis of Algorithms/201-Exhaustive Search.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Design and Analysis of Algorithms/201-Exhaustive Search.md -------------------------------------------------------------------------------- /Design and Analysis of Algorithms/202-Divide and Conquer.md: -------------------------------------------------------------------------------- 1 | # Divide and Conquer 2 | --- 3 | 1. Divide instance of problem into two or more smaller instances => Divide 4 | 2. Solve smaller instances recursively => Conquer 5 | 3. Obtain solution to original instance by combining these solutions => Combine 6 | 7 | > A divide-and-conquer algorithm breaks down a large problem into smaller subproblems which themselves may be broken down into even smaller subproblems, and so on. The recursion bottoms out when it reaches a base case and the subproblem is small enough to solve directly without further recursing 8 | 9 | - Even though binary search does not solve the other half of the subproblem, it can be considered as divide and conquer 10 | 11 | ## Master Theorem 12 | - Provides bounds for recurrences of the form `T(n) = aT(n/b) + f(n)` 13 | 14 | ![[master.png]] 15 | 16 | > Write an algorithm to find the sum of array elements using divide and conquer 17 | > 18 | 19 | ``` 20 | foo(A[0...n-1], n) 21 | if n=1, return A[0] 22 | else foo(A[0...n/2], n/2) + fun(A[(n/2)+1...n-1], n/2) 23 | 24 | T(n) = 2T(n/2) + 1 25 | ``` 26 | 27 | - Decrease and conquer method 28 | ``` 29 | foo(A[0...n-1]) 30 | if n=0, return 0 31 | return foo(A[0...n-2]) + A[n-1] 32 | 33 | T(n) = T(n-1) + 1 34 | ``` 35 | 36 | > Apply brute force, decrease and conquer, divide and conquer to find a^n 37 | 38 | - Brute force 39 | ``` 40 | foo(a, n) 41 | prod = 1 42 | for i=1 to n 43 | prod = prod*a 44 | return prod 45 | ``` 46 | 47 | - Decrease and conquer 48 | ``` 49 | foo(a, n) 50 | if n=1, return a 51 | return a * foo(a, n-1) 52 | 53 | T(n) = T(n-1) + 1 54 | ``` 55 | 56 | - Divide and conquer 57 | ``` 58 | foo(a, n) 59 | if n=1, return a 60 | if n is odd, return a * foo(a, (n-1)/2) * foo(a, (n-1)/2) 61 | return foo(a, n/2) * foo(a, n/2) 62 | 63 | T(n) = 2T(n/2) + 1 64 | ``` 65 | 66 | ## Merge Sort 67 | 68 | > For a given array, find the method to sort the elements which takes nlog(n) in the worst case with or without an extra space 69 | 70 | ``` 71 | Mergesort(A[0...n-1]) 72 | if n>1 73 | copy A[0...(n/2)-1] to B[0...(n/2)-1] 74 | copy A[(n/2)...n-1] to C[0...(n/2)-1] 75 | Mergesort(B[0...(n/2)-1]) 76 | Mergesort(C[0...(n/2)-1]) 77 | Merge(B,C,A) 78 | 79 | Merge(B[0...p-1], C[0...q-1], A[0...p+q-1]) 80 | i=0, j=0, k=0 81 | while i

subroutines which don't need to save can be called to return very quickly. Disadvantage => all other routines have the overhead of explicitly saving to R14 5 | - To return from a subroutine, the program simply has to move `R14` back into `R15`: `MOV R15, R14` or `MOV PC, LR` 6 | 7 | ``` 8 | MOV R1, #3 9 | BL Foo 10 | ADD R2, R0, R1 11 | SWI 0x11 12 | 13 | Foo: 14 | MOV R0, #2 15 | MOV PC, LR 16 | ``` 17 | 18 | ``` 19 | /* Parameter passing using stack */ 20 | 21 | .data 22 | A: .word 0 23 | 24 | .text 25 | LDR R6, =A 26 | MOV R1, #25 ; parameter 1 27 | MOV R2, #25 ; parameter 2 28 | STMFD R13!, {R1, R2}; parameters are pushed on stack 29 | BL Link 30 | STR R0, [R6] ; store result 31 | SWI 0x11 32 | 33 | Link: 34 | LDMFD R13!, {R4, R5} ;parameters are popped from the stack 35 | ADD R0, R4, R5 36 | MOV PC, LR 37 | ``` 38 | 39 | ``` 40 | /* Nested Procedure Call: Multiply(Add(a,b), c) */ 41 | 42 | .data 43 | A: .word 0 44 | 45 | .text 46 | LDR R4, =A 47 | 48 | MOV R1, #11 49 | MOV R2, #10 50 | MOV R3, #02 51 | STMFD R13!, {R1-R3} 52 | 53 | BL AddFunc ; call to AddFunc 54 | STR R0, [R4] 55 | SWI 0x11 56 | 57 | AddFunc: 58 | LDMFD R13!, {R5-R7} 59 | ADD R0, R5, R6 60 | STMFD R13!, {R0, R6, LR} 61 | BL MulFunc ; call to MulFunc 62 | MOV PC, LR ; return to main procecdure 63 | 64 | MulFunc: 65 | LDMFD R13!, {R4, R5, LR} 66 | MUL R0, R4, R5 67 | MOV PC, LR ; return to AddFunc 68 | 69 | ``` -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/201-Pipeline Hazards.md: -------------------------------------------------------------------------------- 1 | # Pipeline Hazards 2 | --- 3 | - Hazards prevent the next instruction in the instruction stream from executing during its designated clock cycle 4 | - They reduce the performance from the ideal speedup gained by pipelining 5 | - There are 3 classes of hazards: 6 | 1. Structural hazards arise from resource conflicts when the hardware cannot support all possible combinations of instructions simultaneously in overlapped execution 7 | 2. Data hazards arise when an instruction depends on the results of a previous instruction in a way that is exposed by overlapping of instructions in the pipeline 8 | 3. Control hazards arise from the pipelining of branches and other instructions that change the PC 9 | - Hazards in pipelines can make it necessary to stall the pipeline 10 | 11 | ![[pipeline1.png]] 12 | ![pipeline1](https://github.com/Shogunkayo/PES_Notes/blob/main/Microprocessor%20and%20Computer%20Architecture/Images/pipeline1.png) 13 | 14 | ![[pipeline2.png]] 15 | ![pipeline2](https://github.com/Shogunkayo/PES_Notes/blob/main/Microprocessor%20and%20Computer%20Architecture/Images/pipeline2.png) 16 | 17 | ![[pipeline3.png]] 18 | ![pipeline3](https://github.com/Shogunkayo/PES_Notes/blob/main/Microprocessor%20and%20Computer%20Architecture/Images/pipeline3.png) 19 | 20 | ## Structural Hazards 21 | - When a processor is pipelined, the overlapped execution of instructions requires pipelining of functional units and duplication of resources to allow all possible combinations of instructions in the pipeline 22 | - If some combination of instructions cannot be accomodated because of resource conflicts, the processor is said to have a structural hazard 23 | - The most common instances of structural hazards arise when some functional unit is not fully pipelined and when some resource has not been duplicated enough to allow all combinations of instructions in the pipeline to execute 24 | - For example, a processor may have only one register-file write port, but under certain circumstances, the pipeline might want to perform two writes in a clock cycle 25 | - To resolve this hazard, we stall the pipeline for 1 clock cycle when the data memory access occurs 26 | - A stall is commonly called a pipeline bubble 27 | 28 | ## Data Hazards 29 | - Data hazards occur when the pipeline changes the order of read/write accesses to operands so that the order differs from the order seen by sequentially executing instructions on an unpipelined processor 30 | - Data hazards can be minimized by forwarding 31 | 1. The ALU result from both the EX/MEM and MEM/WB pipeline registers is always fed back to the ALU inputs 32 | 2. If the forwarding hardware detects that the previous ALU operation has written the register corresponding to a source for the current ALU operation, control logic selects the forwarded result as the ALU input rather than the value read from the register file 33 | 34 | ## Branch Hazards 35 | -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/300-Memory.md: -------------------------------------------------------------------------------- 1 | # Memory 2 | --- 3 | CPU basic components: 4 | - RAM (latches and decoder) 5 | - Register 6 | - Instruction Register 7 | - Program Counter 8 | - Accumulator 9 | - Buses 10 | - Address 11 | - Data 12 | - Control 13 | 14 | - Memory latency is the time it takes to transfer a word of data to or from memory 15 | - Memory bandwidth is the number of bits or bytes that can be transferred in one second 16 | 17 | ## Speed vs Size vs Cost 18 | Fast memory is more expensive per bit than slower 19 | 90/10 rule => a program spends 90% of its time in 10% of its code 20 | 21 | ## CPU Cache 22 | - Cache memory is an architectural arrangement which makes the main memory appear faster to the processor than it really is 23 | - Level 3 > 2 > 1 in size, 1 > 2 > 3 in speed 24 | 25 | ## Cache Terminology 26 | Block -> Main memory 27 | Line -> Cache memory 28 | 29 | - Block placement 30 | - Block identification 31 | - Block replacement 32 | - Write strategy 33 | 34 | - 3 mapping 35 | 1. Direct mapping -> Block(i) % no of lines 36 | - If there is free space in the cache, not possible to use it 37 | - 4 bit for word, 5bit for tag, 7bit address 38 | 1. Associative mapping -> divide by 39 | - K way division 40 | - Only word and tag 41 | - Time complexity is higher as it needs to check all lines 42 | 1. Set-associative mapping 43 | - K way sets 44 | - Combination of direct and associative 45 | 46 | ## Least Recently Used 47 | Random simple to buiild in the hardware 48 | LRU becomes expensive as the number of blocks increases 49 | 50 | Consider a fully associative mapped cache of size 16KB with block size 256bytes. The size of main memory is 128KB. Find number of tag bits 51 | 52 | # Replacement Policies 53 | --- 54 | -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/3stage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/3stage.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/3stage1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/3stage1.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/3stage2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/3stage2.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/5stage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/5stage.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/Capture.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/Capture.PNG -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/addrmode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/addrmode.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/barrelshift.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/barrelshift.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/byteaddr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/byteaddr.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/cpsr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/cpsr.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr1.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr10.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr11.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr12.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr2.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr3.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr4.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr5.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr6.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr7.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr8.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instr9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instr9.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/instrusage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/instrusage.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/memaddr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/memaddr.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/mul.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/mul.PNG -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/multrans.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/multrans.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/multrans2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/multrans2.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/pipeline1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/pipeline1.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/pipeline2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/pipeline2.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/pipeline3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/pipeline3.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/progstruct.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/progstruct.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/psr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/psr.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/psr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/psr2.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/reg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/reg.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/swi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/swi.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/swi2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/swi2.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/swi3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/swi3.png -------------------------------------------------------------------------------- /Microprocessor and Computer Architecture/Images/trans.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Microprocessor and Computer Architecture/Images/trans.PNG -------------------------------------------------------------------------------- /Operating Systems/301-Swapping.md: -------------------------------------------------------------------------------- 1 | # Swapping 2 | ---- 3 | A process must be in memory to be executed. It can be swapped temporarily out of memory to a backing store and then brought back into memory for continued execution. Swapping makes it possible for the total physical address space of all processes to exceed the real physical memory of the system, thus increasing the degree of multiprogramming in a system 4 | 5 | ## Standard Swapping 6 | - Involves moving processes between main memory and a backing store 7 | - The backing store is commanly a fast disk. It must be large enough to accomodate copies of all memory images for all users, and it must provide direct access to these memory images 8 | - The system maintains a ready queue consisting of all processes whose memory images are on the backing store or in memory and are ready to run 9 | - Whenever the CPU scheduler decides to execute a process, it calls the dispatcher. 10 | - The dispatcher checks to see whether the next process in the queue is in memory. If it is not, and if there is no free memory region, the dispatcher swaps out a process currently in memory and swaps in the desired process. It then reloads registers and transfers control to the selected process. 11 | 12 | ![[backingstore.png]]![backingstore](https://github.com/Shogunkayo/PES_Notes/blob/main/Operating%20Systems/images/backingstore.png) 13 | 14 | - The context switch time in such a swapping system is fairly high. Total swap time = swap in time + swap out time 15 | - The major part of the swap time is transfer time. The total transfer time is directly proportional to the amount of memory swapped 16 | - A process with dynamic memory requirements will need to issue system calls (`request_memory()` and `release_memory()`) to inform the operating system of its changing memory needs 17 | - Only an idle process can be swapped. If an operation is asynchronously accessing the user memory, then the process cannot be swapped. 18 | - Transfers between operating system buffers and process memory occur only when the process is swapped in. This double buffering adds overhead as we now need to copy the data again, from kernel memory to user memory, before the user process can access it 19 | - Standard swapping is not used in modern operating systems as it requires too much swapping time and provides too little execution time to be a reasonable memory management solution 20 | - In one variation, swapping is normal disabled but will start if the amount of free memory falls below a threshold amount. Swapping is halted when the amount of free memory increases 21 | - Another variation involves swapping portions of processes rather than entire processes, to decrease swap time 22 | 23 | ## Swapping on Mobile Systems 24 | - Mobile systems typically do not support swapping in any form 25 | - Mobile devices generally use flash memory rather than spacious hard disks as their persistent storage, resulting in memory space constraint 26 | - There is also a limited number of writes that flash memory can tolerate before it becomes unreliable 27 | - There is also a limited number of writes that flash memory can tolerate before it becomes unreliable -------------------------------------------------------------------------------- /Operating Systems/303-Segmentation.md: -------------------------------------------------------------------------------- 1 | # Segmentation 2 | --- 3 | - Memory can be viewed as a collection of variable sized segments, with no necessary ordering among the segments 4 | - Segmentation is a memory management scheme that supports this view of memory. 5 | - A logical address is a collection of segments. Each segment has a name and a length. The address specify both the segment name and the offset within the segment 6 | - The programmer specifies each address by two quantities: a segment name and an offset. For simplicity of implementation, segments are numbered and are referred to by a segment number rather than by a segment name. 7 | - Logical address consists of a two tuple: `` 8 | - Libraries that are linked in during compile time might be assigned seperate segments. The loader would take all 9 | 10 | ## Segmentation Hardware 11 | - Although a programmer can now refer to objects in the program by a two dimensional address, the actual physical memory is still a one dimensional sequence of bytes 12 | - The mapping is effected by a segment table 13 | - Each entry in the segment table has a segment base and a segment limit. The segment base contains the starting physical address where the segment resides in memory, and the segment limit specifies the length of the segment 14 | 15 | ![[segmenttable.png]] 16 | ![segmenttable](https://github.com/Shogunkayo/PES_Notes/blob/main/Operating%20Systems/images/segmenttable.png) 17 | 18 | - Logical address consists of two parts: a segment number `s` and an offset into that segment `d` 19 | - The segment number is used as an index to the segment table 20 | - The offset must be between 0 and the segment limit. 21 | - When the offset is legal, it is added to the segment base to produce the address in physical memory of the desired byte 22 | - If it is illegal, it will result in a trap to the operating system -------------------------------------------------------------------------------- /Operating Systems/images/addr_prot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/addr_prot.png -------------------------------------------------------------------------------- /Operating Systems/images/backingstore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/backingstore.png -------------------------------------------------------------------------------- /Operating Systems/images/basicmet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/basicmet.png -------------------------------------------------------------------------------- /Operating Systems/images/codeshare.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/codeshare.png -------------------------------------------------------------------------------- /Operating Systems/images/hashedpage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/hashedpage.png -------------------------------------------------------------------------------- /Operating Systems/images/invertedpage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/invertedpage.png -------------------------------------------------------------------------------- /Operating Systems/images/logphys.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/logphys.png -------------------------------------------------------------------------------- /Operating Systems/images/memory_space.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/memory_space.png -------------------------------------------------------------------------------- /Operating Systems/images/multistep.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/multistep.png -------------------------------------------------------------------------------- /Operating Systems/images/pagemodel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/pagemodel.png -------------------------------------------------------------------------------- /Operating Systems/images/pagetable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/pagetable.png -------------------------------------------------------------------------------- /Operating Systems/images/segmenttable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/segmenttable.png -------------------------------------------------------------------------------- /Operating Systems/images/tlb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/tlb.png -------------------------------------------------------------------------------- /Operating Systems/images/twolevel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/twolevel.png -------------------------------------------------------------------------------- /Operating Systems/images/twolevel1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/twolevel1.png -------------------------------------------------------------------------------- /Operating Systems/images/twolevel2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/twolevel2.png -------------------------------------------------------------------------------- /Operating Systems/images/twolevel3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/twolevel3.png -------------------------------------------------------------------------------- /Operating Systems/images/valinval.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/Operating Systems/images/valinval.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PES Notes 2 | Notes I made for the courses I took in PES University 3 | 4 | ## Ongoing Courses and Status 5 | 1. Computer Networks 6 | - Currently in Unit 3 7 | 8 | 2. Microprocessor and Computer Architecture 9 | - Currently in Unit 2 10 | 11 | 3. Design and Application of Algorithms 12 | - Currently in Unit 2 13 | - I haven't made notes for Unit 1, will probably do near sem end 14 | 15 | 4. Operating Systems 16 | - Currently in Unit 3 17 | - I haven't made notes for Unit 1 and 2, will probably do near sem end 18 | 19 | ## Completed Courses and Status 20 | 1. Data Structures and Applications 21 | - Have covered most (if not all) of the topics upto unit 4. 22 | - Please refer a different source for unit 5 as I probably wont be updating. 23 | 24 | 2. Web Technologies 25 | - Just no. 26 | 27 | -------------------------------------------------------------------------------- /WebTech/00-Web Development.md: -------------------------------------------------------------------------------- 1 | # INDEX 2 | --- 3 | 4 | 1. HTML and CSS: [[A0-HTML&CSS]] 5 | 2. JavaScript: [[B0-JavaScript]] 6 | 3. JQuery: [[C0-JQuery]] 7 | 4. -------------------------------------------------------------------------------- /WebTech/A1-Media Queries.md: -------------------------------------------------------------------------------- 1 | # RESPONSIVE DESIGN 2 | ---- 3 | 4 | - Media queries tell the browser how to style an element at a particular viewpoint dimension 5 | - Viewport meta tag tells the browser what width the viewpoint should be 6 | 7 | ### Setting up the viewport 8 | ``` 9 | 10 | ``` 11 | 12 | ### Making media queries 13 | ``` 14 | @media screen and (max-width: 700px){ 15 | .banner .welcome{ 16 | position: relative; 17 | text-align: center; 18 | } 19 | .banner .welcome h2{ 20 | font-size: 20px; 21 | } 22 | .banner .welcome br{ 23 | display: none; 24 | } 25 | .banner .welcome span{ 26 | font-size: 1em; 27 | } 28 | .images li{ 29 | width: 100%; 30 | margin: 20px auto; 31 | display: block; 32 | } 33 | } 34 | ``` 35 | 36 | https://www.youtube.com/watch?v=Xig7NsIE6DI -------------------------------------------------------------------------------- /WebTech/A2-Audio_Video.md: -------------------------------------------------------------------------------- 1 | # AUDIO 2 | --- 3 | - `

17 | 32 | ``` 33 | -------------------------------------------------------------------------------- /WebTech/B5-Web_Workers.md: -------------------------------------------------------------------------------- 1 | # WEB WORKERS 2 | --- 3 | - Thread which executes a JavaScript file 4 | - Makes it possible to execute JavaScript files asynchronously and autonomously 5 | - Helps achieve multi threading and improving performance 6 | - Web worker works in the background, independent of other scripts 7 | 8 | - Web workers are created and executed in the main file, while their implementations are in a seperate JavaScript file 9 | - In the main file, 10 | ``` 11 | var new_work = new Worker("url to the code file") 12 | ``` 13 | 14 | - The parent code may also have a callback function that listens for a response from the worker 15 | In the main file 16 | ``` 17 | let worker = new Worker("webwork.js"); 18 | worker.postMessage("Hello"); 19 | 20 | worker.addEventListener('message', (e)=>{ 21 | console.log("Worker said: ", e.data); 22 | }, false); 23 | ``` 24 | In "webwork.js" 25 | ``` 26 | self.addEventListener('message', (e)=>{ 27 | self.postMessage(e.data); 28 | }, false); 29 | ``` 30 | 31 | - Web workers do not have access to the DOM tree of the main file 32 | 33 | ## Applications 34 | - Listen for messages using the `onmessage` event listener function 35 | - Send messages via `postMessage()` method 36 | - Send AJAX requests using the XMLHttpRequest 37 | - Create timers using `setTimeout()` and `sendInterval()` methods 38 | 39 | ## Types 40 | 1. Dedicated Worker 41 | - Instantiated by the main process 42 | - Only accessible by the script that called it 43 | 44 | 2. Shared Worker 45 | - Can be reached by all processes running on the same origin 46 | - Can be accessed by multiple scripts 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /WebTech/B6-Files.md: -------------------------------------------------------------------------------- 1 | # FILE API 2 | --- 3 | - HTML5 allows JavaScript to load and process files from the local file system 4 | 5 | | Core Objects | Description | 6 | | ------------ | ----------------------------------------------------------------------------------- | 7 | | File | object representing a file in the local file system | 8 | | FileList | list of files | 9 | | Blob | represent a Binary Large Object which is used to hold the contents of a single file | 10 | 11 | [Further Information](https://developer.mozilla.org/en-US/docs/Web/API/File) 12 | -------------------------------------------------------------------------------- /WebTech/C0-JQuery.md: -------------------------------------------------------------------------------- 1 | # JQUERY 2 | ---- 3 | - A lightweight, "write less, do more" and open source JavaScript library that simplifies programming 4 | - Purpose is to make JavaScript easier to use 5 | - Released in Jan 2006 at BarCampNYC by John Resig 6 | 7 | - HTML elements are "queried" and actions are performed on them 8 | - Basic syntax, 9 | ``` 10 | $(selector).action() 11 | ``` 12 | 13 | - Example, to hide all the elements with id "test" 14 | ``` 15 | $("#test").hide() 16 | ``` 17 | - To do the above task in JavaScript, 18 | ``` 19 | document.getElementByID("test").style.display="none"; 20 | ``` 21 | 22 | 23 | ## Document Ready Event 24 | - All JQuery code are written inside this event to prevent running before the document has fully loaded 25 | ``` 26 | $(document).ready(function(){ 27 | // code 28 | }); 29 | ``` 30 | 31 | -------------------------------------------------------------------------------- /WebTech/Images/complex_component.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/complex_component.png -------------------------------------------------------------------------------- /WebTech/Images/dom1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/dom1.png -------------------------------------------------------------------------------- /WebTech/Images/dom2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/dom2.png -------------------------------------------------------------------------------- /WebTech/Images/events.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/events.png -------------------------------------------------------------------------------- /WebTech/Images/events1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/events1.png -------------------------------------------------------------------------------- /WebTech/Images/events2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/events2.png -------------------------------------------------------------------------------- /WebTech/Images/events3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/events3.png -------------------------------------------------------------------------------- /WebTech/Images/geo1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/geo1.png -------------------------------------------------------------------------------- /WebTech/Images/mouse1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/mouse1.png -------------------------------------------------------------------------------- /WebTech/Images/react_dom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shogunkayo/PES_Notes/d86f01bd1c54a3ae08a4c80307428d87ad39f02b/WebTech/Images/react_dom.png -------------------------------------------------------------------------------- /WebTech/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /WebTech/ReactJS/Event_Handling.md: -------------------------------------------------------------------------------- 1 | # EVENT HANDLING 2 | --- 3 | - Event make the web app interactive and responsive to the user 4 | 5 | | JSX | DOM | 6 | | ------------------------------------------------------- | ------------------------- | 7 | | pass function as an event handler | pass function as a string | 8 | | event name is camelCase | event name is lowercase | 9 | | have to use preventDefault to prevent default behaviour | function can return false to prevent default behaviour | 10 | 11 | - List of React events 12 | ![[react_events.png]] 13 | 14 | - As class methods are not bound by default, its necessary to bind functions to the class instance to prevent `this` from returning `undefined` 15 | - Event handling system is called as Synthetic Event 16 | - Event handlers are registered at the time of rendering 17 | 18 | Example program to create a counter 19 | ``` 20 | class Counter extends React.Component{ 21 | constructor(props){ 22 | super(props); 23 | this.state = {count:0} 24 | } 25 | 26 | render(){ 27 | return( 28 |
29 |

{this.state.count}

30 | 31 |
32 | ) 33 | } 34 | 35 | increment = () =>{ 36 | this.setState((prevState) => ({count: prevState.count + 1})) 37 | } 38 | } 39 | 40 | ReactDOM.render(, document.getElementById("container")); 41 | ``` -------------------------------------------------------------------------------- /WebTech/ReactJS/Form_Handling.md: -------------------------------------------------------------------------------- 1 | # FORM HANDLING 2 | ---- 3 | 1. Uncontrolled Components 4 | - traditional HTML form inputs which remember what you have typed 5 | - refs are used to get form values 6 | - `defaultValue` is used to specify initial value in React 7 | 2. Controlled Components 8 | - render a form and control what happens in the form 9 | - `state` is used to handle input value and `setState` is used to change the input value 10 | 11 | Example for uncontrolled component 12 | ``` 13 | class UncontrolledComponent extends React.Component{ 14 | constructor(props){ 15 | super(props); 16 | this.handleSubmit = this.handleSubmit.bind(this); 17 | this.input = React.createRef(); 18 | } 19 | 20 | handleSubmit(event){ 21 | event.preventDefault(); 22 | alert("Submitted"+this.input.current.value); 23 | } 24 | 25 | render(){ 26 | return( 27 |
28 | Name: 29 | 30 |
31 | ); 32 | } 33 | } 34 | ``` 35 | 36 | Example for controlled component 37 | ``` 38 | class ControlledComponent extends React.Component{ 39 | constructor(props){ 40 | super(props); 41 | this.state = {value: ''}; 42 | } 43 | 44 | handleChange = (event) => { 45 | this.setState({value:event.target.value}); 46 | } 47 | 48 | handleSubmit = (event) => { 49 | event.preventDefault(); 50 | alert("Submitted"+this.state.value); 51 | } 52 | 53 | render(){ 54 | return( 55 |
56 | Name: 57 | 58 |
59 | ); 60 | } 61 | } 62 | ``` 63 | 64 | To handle mulitple inputs, `this.state.form` is used 65 | ``` 66 | class MultipleInputs extends React.Component{ 67 | constructor(props){ 68 | super(props); 69 | this.state = {form: {name: '', email: ''}} 70 | } 71 | 72 | render(){ 73 | return( 74 |
75 | Name: 76 | Email: 77 | 78 |
79 | ) 80 | } 81 | 82 | handleChange = (event) => { 83 | var input_name = event.target.name; 84 | var input_email = event.target.email; 85 | this.setState({ 86 | ...this.state.form, 87 | form:{ 88 | ...this.state.form, 89 | [input_name]:[input_email] 90 | } 91 | }) 92 | } 93 | 94 | handleSubmit = (event) => { 95 | event.preventDefault(); 96 | alert("Submitted"+this.state.form.name+this.state.form.email); 97 | } 98 | } 99 | ``` -------------------------------------------------------------------------------- /WebTech/ReactJS/Lifecycle_Methods.md: -------------------------------------------------------------------------------- 1 | # LIFECYCLE METHODS 2 | --- 3 | - Series of events that happen from the staring of the React component to its ending 4 | 1. Mounting - birth of the component 5 | 2. Updating - growth of the component 6 | 3. Unmounting - end of the component 7 | 8 | ### Methods 9 | 1. `constructor()` 10 | - premounting phase 11 | - `super()` is called so that the properties of `React.Component` is inherited and additional functionalities can be added 12 | - If `props` is not initialized in the constructor, it might lead to errors 13 | 14 | 2. `componentWillMount()` 15 | - called only once in the lifecycle 16 | - executed before rendering on both client and server side 17 | - useful in scenarios where the date and time has to be set depending on the client 18 | ``` 19 | componentWillMount(){ 20 | this.setState({startDateTime: new Date(Date.now())}); 21 | } 22 | ``` 23 | 24 | 3. `render()` 25 | - Handles rendering of the component 26 | - Accesses `this.state` and `this.props` 27 | 28 | 4. ` componentDidMount()` 29 | - called only once, immediately after the component is rendered 30 | - indicates that the HTML of the component has been rendered into the DOM and can be accessed 31 | 32 | 5. `componentWillReceiveProps()` 33 | - allows to match incomming props with the current props 34 | - invoked as soon as props are updated 35 | 36 | 6. `shouldComponentUpdate()` 37 | - allows a component to exit the update lifecycle 38 | 39 | 1. `componentWillUpdate()` 40 | - called just before rendering while updating a component 41 | - will not be invoked if `shouldComponentUpdate` returns false 42 | 43 | 8. `componentDidUpdate()` 44 | - invoked immediately after update 45 | - not called during initial render 46 | - will not be invoked if `shouldComponentUpdate` returns false 47 | 48 | 9. `componentWillUnmount()` 49 | - last method to be called, before the component is removed from the DOM 50 | - used to perform clean-up of DOM elements and timers created 51 | ``` 52 | componentWillUnmount(){ 53 | clearInterval(this.interval); 54 | } 55 | ``` 56 | 57 | - `ReactDOM.unmountComponentAtNode(node)` is used to delete the component from the DOM 58 | - `componentWillUpdate()`, `componentWillMount()` has been replaced by `getSnapshotBeforeUpdate()` and `componentWillRecieveProps()` has been replaced by `getDerivedStateFromProps()`; the older versions are marked unsafe for use 59 | -------------------------------------------------------------------------------- /WebTech/ReactJS/Stateless_Components.md: -------------------------------------------------------------------------------- 1 | # STATELESS COMPONENTS 2 | --- 3 | - Does not use `state` and does not have a lifecycle 4 | - Usually contain only the `render` method 5 | - For performance and clarity of code, such components should be written as functions instead of classes 6 | - The `render()` method itself can be made into a component 7 | 8 | *A functional component is always a stateless component but a class component can be either a stateful or a stateless component* 9 | 10 | | Stateful | Stateless | 11 | | ---------------------------------------------------------------------- | ----------------------------------------- | 12 | | Also called container, smart components | Also called presentation, dumb components | 13 | | Have a state | Do not have a state | 14 | | Can render both props and states: {this.props.name}, {this.state.name} | Can render only props: {props.name} | 15 | | Always a class component | Can be a class or function component | 16 | 17 | #### Use cases for stateless: 18 | - just to present the props 19 | - dont need an internal variable 20 | - create non interactive element 21 | - want reusable code 22 | 23 | #### Use cases for stateful: 24 | - create an element which accepts user input 25 | - element is interactive 26 | - dependent on state for rendering 27 | - dependent on data that cannot be passed down as props 28 | 29 | ### Creation 30 | ``` 31 | const NewComponent = (props) => (code with implicit return) 32 | ``` 33 | - This method only works for a single expression 34 | 35 | ``` 36 | function NewComponent(props){ 37 | code with explicit return 38 | } 39 | ``` 40 | 41 | Example showcasing the use of `props.children` in a stateless component 42 | ``` 43 | function TestComponent(props){ 44 | return

Welcome, {props.children}

45 | } 46 | 47 | ReactDOM.render(User, document.getElementById("container")); 48 | ``` 49 | 50 | ### Nesting 51 | ``` 52 | function ComponentOne(props){ 53 | var greeting = "Welcome" 54 | return {greeting}, {props.text} 55 | } 56 | 57 | function ComponentTwo(props){ 58 | return

{props.children}

59 | } 60 | 61 | ReactDOM.render( 62 |
63 | 64 | 65 |
, document.getElementById("container")); 66 | ``` 67 | 68 | The above code displays the output, 69 | ``` 70 | Welcome, Hello 71 | Welcome, World 72 | ``` 73 | 74 | -------------------------------------------------------------------------------- /WebTech/ReactJS/States.md: -------------------------------------------------------------------------------- 1 | # States 2 | ---- 3 | - The `state` object is where property values belonging to an object are stored 4 | - When `state` object changes, the component is re-rendered 5 | - It can only be accessed and modified within the component 6 | - A component should have an intial state 7 | 8 | | State | Props | 9 | | ---------------------------------------------------------------------------- | --------------------------- | 10 | | Mutable | Immutable | 11 | | Can be used in class components and function components by using React Hooks | No limitations | 12 | | Generally updated by event handlers | Set by the parent component | 13 | 14 | ``` 15 | class MyComponent extends React.Component{ 16 | constructor(props){ 17 | super(props); 18 | this.state = {attribute1 : 'value1', attributeN: 'valueN'}; 19 | } 20 | } 21 | ``` 22 | - The attributes are accessed inside the component like, 23 | ``` 24 | this.state.attribute1 25 | ``` 26 | 27 | ``` 28 | class Car extends React.Component { 29 | constructor(props) { 30 | super(props); 31 | this.state = { 32 | brand: "Ford", 33 | model: "Mustang", 34 | color: "red", 35 | year: 1964 36 | }; 37 | } 38 | render() { 39 | return ( 40 |
41 |

My {this.state.brand}

42 |

43 | It is a {this.state.color} 44 | {this.state.model} 45 | from {this.state.year}. 46 |

47 |
48 | ); 49 | } 50 | } 51 | ``` 52 | 53 | - The state must always be updated using `setState()` method 54 | ``` 55 | changeColor(){ 56 | this.setState({color: "blue"}); 57 | } 58 | ``` 59 | 60 | Example:- Constructing a Digital Clock in React 61 | ``` 62 | class DigitalClock extends React.Component{ 63 | constructor(props){ 64 | super(props); 65 | this.state var buttonColor = { 66 | 67 | }= {time: new Date()} 68 | } 69 | 70 | tick = () => { 71 | this.setState({time: new Date()}); 72 | } 73 | 74 | render(){ 75 | setInterval(this.tick, 1000) 76 | return(

{this.state.time.toLocaleTimeString()}

) 77 | } 78 | } 79 | ``` 80 | - `this.state.time.toLocaleTimeString()` is a built-in method which returns the time of the local environment as a string 81 | 82 | Example:- To update an element on the press of a button 83 | ``` 84 | class ColorChanger extends React.Component{ 85 | constructor(props){ 86 | super(props); 87 | this.state = {name: "yellow", id:"1"} 88 | } 89 | 90 | click = () => { 91 | this.setState({name: "green", id:"2"}) 92 | } 93 | 94 | render(){ 95 | var buttonColor = { 96 | fontSize: "40px", 97 | backgroundColor: this.state.name 98 | } 99 | return( 100 |
101 |

{this.state.name}

102 | 103 |
104 | ) 105 | } 106 | } 107 | ``` --------------------------------------------------------------------------------