diff --git a/Assignment_3/Assignment_3.xcodeproj/project.pbxproj b/Assignment_3/Assignment_3.xcodeproj/project.pbxproj new file mode 100644 index 0000000..5ebf7f5 --- /dev/null +++ b/Assignment_3/Assignment_3.xcodeproj/project.pbxproj @@ -0,0 +1,299 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 55; + objects = { + +/* Begin PBXBuildFile section */ + CB9FDE3527A3B44A006DC57F /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9FDE3427A3B44A006DC57F /* Node.swift */; }; + CB9FDE3727A3D3A9006DC57F /* Operation.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9FDE3627A3D3A9006DC57F /* Operation.swift */; }; + CB9FDE3927A40A72006DC57F /* Utilty.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9FDE3827A40A72006DC57F /* Utilty.swift */; }; + CBA4C30127A2BB0E0040FA47 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBA4C30027A2BB0E0040FA47 /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + CBA4C2FB27A2BB0E0040FA47 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + CB9FDE3427A3B44A006DC57F /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = ""; }; + CB9FDE3627A3D3A9006DC57F /* Operation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Operation.swift; sourceTree = ""; }; + CB9FDE3827A40A72006DC57F /* Utilty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilty.swift; sourceTree = ""; }; + CBA4C2FD27A2BB0E0040FA47 /* Assignment_3 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Assignment_3; sourceTree = BUILT_PRODUCTS_DIR; }; + CBA4C30027A2BB0E0040FA47 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + CBA4C2FA27A2BB0E0040FA47 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + CBA4C2F427A2BB0E0040FA47 = { + isa = PBXGroup; + children = ( + CBA4C2FF27A2BB0E0040FA47 /* Assignment_3 */, + CBA4C2FE27A2BB0E0040FA47 /* Products */, + ); + sourceTree = ""; + }; + CBA4C2FE27A2BB0E0040FA47 /* Products */ = { + isa = PBXGroup; + children = ( + CBA4C2FD27A2BB0E0040FA47 /* Assignment_3 */, + ); + name = Products; + sourceTree = ""; + }; + CBA4C2FF27A2BB0E0040FA47 /* Assignment_3 */ = { + isa = PBXGroup; + children = ( + CBA4C30027A2BB0E0040FA47 /* main.swift */, + CB9FDE3427A3B44A006DC57F /* Node.swift */, + CB9FDE3627A3D3A9006DC57F /* Operation.swift */, + CB9FDE3827A40A72006DC57F /* Utilty.swift */, + ); + path = Assignment_3; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + CBA4C2FC27A2BB0E0040FA47 /* Assignment_3 */ = { + isa = PBXNativeTarget; + buildConfigurationList = CBA4C30427A2BB0E0040FA47 /* Build configuration list for PBXNativeTarget "Assignment_3" */; + buildPhases = ( + CBA4C2F927A2BB0E0040FA47 /* Sources */, + CBA4C2FA27A2BB0E0040FA47 /* Frameworks */, + CBA4C2FB27A2BB0E0040FA47 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Assignment_3; + productName = Assignment_3; + productReference = CBA4C2FD27A2BB0E0040FA47 /* Assignment_3 */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + CBA4C2F527A2BB0E0040FA47 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1320; + LastUpgradeCheck = 1320; + TargetAttributes = { + CBA4C2FC27A2BB0E0040FA47 = { + CreatedOnToolsVersion = 13.2.1; + }; + }; + }; + buildConfigurationList = CBA4C2F827A2BB0E0040FA47 /* Build configuration list for PBXProject "Assignment_3" */; + compatibilityVersion = "Xcode 13.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = CBA4C2F427A2BB0E0040FA47; + productRefGroup = CBA4C2FE27A2BB0E0040FA47 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + CBA4C2FC27A2BB0E0040FA47 /* Assignment_3 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + CBA4C2F927A2BB0E0040FA47 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + CB9FDE3527A3B44A006DC57F /* Node.swift in Sources */, + CBA4C30127A2BB0E0040FA47 /* main.swift in Sources */, + CB9FDE3927A40A72006DC57F /* Utilty.swift in Sources */, + CB9FDE3727A3D3A9006DC57F /* Operation.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + CBA4C30227A2BB0E0040FA47 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 11.5; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + CBA4C30327A2BB0E0040FA47 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 11.5; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Release; + }; + CBA4C30527A2BB0E0040FA47 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + CBA4C30627A2BB0E0040FA47 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + CBA4C2F827A2BB0E0040FA47 /* Build configuration list for PBXProject "Assignment_3" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + CBA4C30227A2BB0E0040FA47 /* Debug */, + CBA4C30327A2BB0E0040FA47 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + CBA4C30427A2BB0E0040FA47 /* Build configuration list for PBXNativeTarget "Assignment_3" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + CBA4C30527A2BB0E0040FA47 /* Debug */, + CBA4C30627A2BB0E0040FA47 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = CBA4C2F527A2BB0E0040FA47 /* Project object */; +} diff --git a/Assignment_3/Assignment_3.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Assignment_3/Assignment_3.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Assignment_3/Assignment_3.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Assignment_3/Assignment_3.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Assignment_3/Assignment_3.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Assignment_3/Assignment_3.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Assignment_3/Assignment_3/Node.swift b/Assignment_3/Assignment_3/Node.swift new file mode 100644 index 0000000..681182e --- /dev/null +++ b/Assignment_3/Assignment_3/Node.swift @@ -0,0 +1,22 @@ +// +// Node.swift +// Assignment_3 +// +// Created by Sanskar on 28/01/22. +// + +import Foundation + +class Node +{ + let id : Int + let name : String + let info : String + + init(_ id : Int , _ name : String, _ info : String) + { + self.id = id + self.name = name + self.info = info + } +} diff --git a/Assignment_3/Assignment_3/Operation.swift b/Assignment_3/Assignment_3/Operation.swift new file mode 100644 index 0000000..7a8c44e --- /dev/null +++ b/Assignment_3/Assignment_3/Operation.swift @@ -0,0 +1,131 @@ +// +// sdfsf.swift +// Assignment_3 +// +// Created by Sanskar on 27/01/22. +// + +import Foundation + +var parent = [Int : Set]() //stores immediate parents of node +var children = [Int : Set]() //stores immediate childrens of node +var nodes = [Int : Node]() + +class Operation +{ + static func addNode() + { + print("Enter the name for the node") + if let name = readLine() + { + print("Enter the additional info for the node") + if let info = readLine() + { + let n = nodes.count //node id + nodes[n] = Node.init(n , name , info) + parent[n] = [] + children[n] = [] + return + } + print(invalidChoice) + } + } + + static var visited = [Int : Bool]() + static func cyclicExist(_ node : Int) -> Bool + { + if(visited[node] == true) + { + return true + } + visited[node] = true + var flag = false + for nodes in parent[node] ?? [] + { + flag = flag || cyclicExist(nodes) + } + return flag + } + + static func addEdge(_ node1 : Int , _ node2 : Int) + { + addEdgeChildren(node1 , node2) + visited.removeAll() + if(cyclicExist(node1)) + { + print("Can not add edge because of cycle existance") + children[node1]?.remove(node2) + return + } + + addEdgeParent(node1 , node2) + } + + static func addEdgeParent(_ node1 : Int , _ node2 : Int) + { + parent[node2]?.insert(node1) + } + + static func addEdgeChildren(_ node1 : Int , _ node2 : Int) + { + children[node1]?.insert(node2) + } + + static func getDescendents(_ node : Int) + { + Utility.nodeInfo(node) + for nodes in children[node] ?? [] + { + getDescendents(nodes) + } + } + + static func getAncestors(_ node : Int) + { + Utility.nodeInfo(node) + for nodes in parent[node] ?? [] + { + getAncestors(nodes) + } + } + + static func getImmediateChild(_ node : Int) + { + Utility.nodeInfo(node) + for nodes in children[node] ?? [] + { + Utility.nodeInfo(nodes) + } + } + + static func getImmediateParent(_ node : Int) + { + Utility.nodeInfo(node) + for nodes in parent[node] ?? [] + { + Utility.nodeInfo(nodes) + } + } + + static func removeEdge(_ node1 : Int , _ node2 : Int) + { + children[node1] = nil + parent[node2] = nil + } + + static func removeNode(_ node : Int) + { + children[node] = nil + parent[node] = nil + nodes[node] = nil + for nodes in children.keys + { + if(children[nodes]!.contains(node)) + { + children[nodes]?.remove(node) + } + } + print("Successfully deleted") + + } +} diff --git a/Assignment_3/Assignment_3/Utilty.swift b/Assignment_3/Assignment_3/Utilty.swift new file mode 100644 index 0000000..d8184e6 --- /dev/null +++ b/Assignment_3/Assignment_3/Utilty.swift @@ -0,0 +1,63 @@ +// +// Utilty.swift +// Assignment_3 +// +// Created by Sanskar on 28/01/22. +// + +import Foundation + +class Utility +{ + static func getNode() -> Int //input one node detail + { + print("Please enter the id of node") + if let nodeTemp = readLine() + { + if let node = (Int)(nodeTemp) + { + if(nodes[node] == nil) + { + print("Node id dont exist") + } + return node + } + } + return -1 + } + + static func getNodes() -> (first : Int , second : Int) //input two node details + { + print("Enter the parent id of Node") + if let n1 = readLine() + { + if let node1 = (Int)(n1) + { + print("Enter the child id of Node") + if let n2 = readLine() + { + if let node2 = (Int)(n2) + { + if(nodes[node1] == nil || nodes[node2] == nil) + { + print("Invalid node id") + } + else + { + return (node1 , node2) + } + } + } + } + print(invalidChoice) + } + return (-1 , -1) + } + + static func nodeInfo(_ node : Int) + { + print("Node id = \(nodes[node]!.id) ") + print("Node name = \(nodes[node]!.name) ") + print("Node info = \(nodes[node]!.info) ") + } +} diff --git a/Assignment_3/Assignment_3/main.swift b/Assignment_3/Assignment_3/main.swift new file mode 100644 index 0000000..a427a1e --- /dev/null +++ b/Assignment_3/Assignment_3/main.swift @@ -0,0 +1,110 @@ + +// +// main.swift +// assignment1 +// +// Created by Sanskar on 11/01/27. +// +import Foundation + +let invalidChoice = "Invalid Input Choosen , Please Enter Valid Input" + +func main() +{ + var temp = true // variable to know if user select exit as option + repeat + { + print(""" + CHOOSE THE OPTION FOR OPERATION : + + 1. Add Node to the graph + 2. Add dependency to the graph + 3. Delete node to the graph + 4. Delete dependency to the graph + 5. Get descendants of node + 6. Get ancestors of node + 7. Get immediate parents + 8. Get immediate children + 9. Exit + + Enter your Choice + """) + if let input = readLine() + { + if let choice = (Int)(input) + { + switch choice + { + case 1 : + Operation.addNode() + + case 2 : + let (node1, node2) = Utility.getNodes() + if(node1 != -1 && node2 != -1) + { + Operation.addEdge(node1 , node2) + } + + case 3 : + let node = Utility.getNode() + if(node != -1) + { + Operation.removeNode(node) + } + + case 4 : + let node1 = Utility.getNodes().first + let node2 = Utility.getNodes().second + if(node1 != -1 && node2 != -1) + { + Operation.removeEdge(node1 , node2) + } + + case 5 : + let node = Utility.getNode() + if(node != -1) + { + Operation.getDescendents(node) + } + + case 6 : + let node = Utility.getNode() + if(node != -1) + { + Operation.getAncestors(node) + } + + case 7 : + let node = Utility.getNode() + if(node != -1) + { + Operation.getImmediateParent(node) + } + + case 8 : + let node = Utility.getNode() + if(node != -1) + { + Operation.getImmediateChild(node) + } + + case 9 : + print("THANK YOU") + temp = false + + default : + print(invalidChoice) + } + } + } + + else + { + print(invalidChoice) + } + + }while(temp) +} + +main() +