Posts

Showing posts from July, 2020

Graph Theory(Breadth First Search)

Image
Hellllo, guys new article again WOwwwww  in this article I will talk about another search way in  graph topic, which is DFS.    first, let's speak about how it's working?? ok, as you see now in this image Bfs is working by search level by level it searches in the level one  first then when it's complete level one it starts a search in the next level and so on. ( notes that Bfs work in an unweighted graph  or weighted graph with equal-weighted ) ( notes that Bfs is used to search to the shortest path )  EXAMPLE :( maze solver ) now, we wanna ask a question how can i know the level of the node in implementation? we will write this algorithm by using a queue of pair the first one will be the value of the node and the second one will be the level . Like:   vector<int> BFS(int s, vector<vector<int> > & adjList) { vector<int> len(sz(adjList), OO);  //oo is define to infinity queue< pair<int, int> > q; q.push(MP(s, 0)), len[s] = 0; i

Graph Theory(Depth First Search)

Image
Hello, guys in this article I will speak about the important searching way which is used in graph theory.  Depth-first-search, ok first what is it? and how it works? Here we have a photo which describes how this algorithm work:    In this algorithm, we move from the root of the tree to the depth level then we move up again and down again. as you see in the photo;    now one will ask me ok how can I implement that? I will write an Implementation in C++ language void dfs(int node) { visited[node] = true; for(int i=0;i< adj[node];i++) { int child = adj[node][i]; if (!visited[child]) // To avoid cyclic behavior dfs(child); } }    let's talk about Dfs application which is Dfs EdgeClassification . ( notice that it works in Directed graph ) what is it??? let's look first. ok in this photo you will notice four types of edges. the first one is tree Edge : we called that edge is a tree edge when we move from parent to child (like 1  -> 2) the second one is Bac