Graphs: ======= it is a pictorial representation of a set of objects where some pair of objects are connected by the links. ==> The interconnection between the objects are represented as "vertices". ==> links that connects to the vertices are called as "edges". # graph graph = { "a" : ["b","c"], "b" : ["a","d"], "c" : ["a","d"], "d" : ["b","e"], "e" : ["d"] } print(graph) ==================== Displaying of graph Vertices: ============================= class graph: def __init__(self,gdict = None): if gdict is None: gdict = [] self.gdict = gdict def getVertices(self): return list(self.gdict.keys()) graph_structure = { "a" : ["b","c"], "b" : ["a","d"], "c" : ["a","d"], "d" : ["b","e"], "e" : ["d"] } gobj = graph(graph_structure) print("The Vertices of the given graph are:",gobj.getVertices()) ========================================================== Displaying graph edges: ======================= class graph: def __init__(self,gdict = None): if gdict is None: gdict = [] self.gdict = gdict def edges(self): return self.findedges() def findedges(self): edgename = [] for i in self.gdict: # keys vertices for j in self.gdict[i]: # values edges if(j,i) not in edgename: edgename.append({i,j}) return edgename graph_structure = { "a" : ["b","c"], "b" : ["a","d"], "c" : ["a","d"], "d" : ["b","e"], "e" : ["d"] } gobj = graph(graph_structure) print("The Edges = ",gobj.edges()) ================================================== Adding Vertex: ============== class graph: def __init__(self,gdict = None): if gdict is None: gdict = [] self.gdict = gdict def getVertices(self): return list(self.gdict.keys()) def addVertex(self,vertex): if vertex not in self.gdict: self.gdict[vertex] = [] graph_data = { "a" : ["b","c"], "b" : ["a","d"], "c" : ["d","a"], "d" : ["b","e"], "e" : ["d"] } gobj = graph(graph_data) gobj.addVertex("f") print(gobj.getVertices()) =============================================== Adding of an edge to the graph: =============================== class graph: def __init__(self,gdict = None): if gdict is None: gdict = [] self.gdict = gdict def edges(self): return self.findEdges() def findEdges(self): edgename = [] for i in self.gdict: # keys vertices for j in self.gdict[i]: # values edges if (j, i) not in edgename: edgename.append({i, j}) return edgename def addEdge(self,edge): edge = set(edge) (vertex1,vertex2) = tuple(edge) if vertex1 in self.gdict: self.gdict[vertex1].append(vertex2) else: self.gdict[vertex1] = [vertex2] graph_data = { "a" : ["b","c"], "b" : ["a","d"], "c" : ["a","d"], "d" : ["b","e"], "e" : ["d"] } gobj = graph(graph_data) gobj.addEdge({'a','e'}) gobj.addEdge({'a','c'}) print(gobj.edges()) ================================================= Searching Algorithms: ===================== Ecoomerce: Flipkart clothes:Kart(02-10-10pm) phones: Kart(02-10-8pm) beauty products:Kart(03-10-6am) 1) Linear search: ================= # Linear search def linearSearch(array,n,x): for i in range(0,n): if array[i] == x: return i return -1 array = [2,4,0,1,9,7,11,5] x = 100 n = len(array) resultant_position = linearSearch(array,n,x) if resultant_position == -1: print("The Given Searching object is not found in an array.") else: print("The Given Searching object is identifies in an array at:{}".format(resultant_position)) ==================================================== Binary Search: ============== # Binary Search def binarySearch(array, x, low, high): while low <= high: mid = low + (high-low)//2 if x == array[mid]: return mid elif x > array[mid]: low = mid + 1 else: high = mid - 1 return -1 array = [3,4,5,6,7,8,9] x = 8 position = binarySearch(array,x,0,len(array)-1) if position == -1: print("Results not found.") else: print("The Searching object is at:",position)