hangout #6

Upload: aadheeshwar-vijayakumar

Post on 03-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Hangout #6

    1/7

    [22:35] == ashish1294 [67157f4e@gateway/web/freenode/ip.103.21.127.78] has joined #nitkieee[22:37] == medhz [7aac8347@gateway/web/freenode/ip.122.172.131.71] has joined #nitkieee[22:38] == abraham_ [[email protected]] has joined #nitkieee[22:39] == kavya [0e604a59@gateway/web/freenode/ip.14.96.74.89] has joined #nitkieee[22:39] == pvk [6ad898b9@gateway/web/freenode/ip.106.216.152.185] has joined #nitkieee[22:40] == abhi_95 [3d036b78@gateway/web/freenode/ip.61.3.107.120] has joined #nitkieee[22:41] == chinmay_dd [[email protected]] has joined #nitkieee[22:42] == ankit_d [0e601fd8@gateway/web/freenode/ip.14.96.31.216] has joined #nitkieee[22:44] who is pvk ?[22:45] pranav[22:46] shall we start[22:47] yeah[22:47] ok[22:47] yea[22:47] yeah[22:47] k[22:47] yes[22:47] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has joined #nitk

    ieee[22:48] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has quit [ClientQuit][22:48] I am waiting for Mayank, he said he will come today :)[22:48] Just a few more mins[22:50] It's been almost 2 weeks since last hangout ![22:50] Some of you are practicing vigorously which is great, but only some ![22:51] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has joined #nitkieee[22:51] Only 8 people today![22:51] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has quit [ClientQuit]

    [22:52] == mb1994 [[email protected]] has joined #nitkieee[22:52] I hope you have utilized the interim period wisely[22:52] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has joined #nitkieee[22:52] So you already know about graphs[22:53] == aish [75ca1918@gateway/web/freenode/ip.117.202.25.24] has joined #nitkieee[22:54] I will directly start by methods of representing graphs ![22:54] 3 popular methods - Adjacency Matrix, Adjacency List and Edge List ![22:55] Adjacency matrix is nxn matrix with each element either 0 or 1. 0 means there is no edge between nodes and 1 means there is an edge[22:55] Simple Example - http://mathworld.wolfram.com/images/eps-gi

    f/AdjacencyMatrix_1002.gif[22:56] In the first graph node no 1, 2 and 3 are connected to 4, so (1,4) (2,4) (3,4) are all one[22:57] similarly 4 is also connected to 1,2,3 so, (4,1) (4,2) (4,3)[22:57] == surya176 [75c0787e@gateway/web/freenode/ip.117.192.120.126] has joined #nitkieee[22:57] == mb1994 [[email protected]] has left #nitkieee ["Leaving"][22:57] I hope you are getting it ![22:57] yes!

  • 8/12/2019 Hangout #6

    2/7

    [22:57] Yes[22:58] == mb1994 [[email protected]] has joined #nitkieee[22:58] yea[22:58] yes[22:58] yes[22:58] yup[22:58] Some of you already know all this, I want the others to know ![22:59] @abhi_95 : who are you ?[22:59] abhilash[22:59] Okay ![22:59] Second one is a more difficult representation[22:59] Adjacency Matrix[23:00] * List[23:00] It is a one linked list for each node[23:01] Every node is represented as a linked list of nodes it is connected to ![23:01] == varsha [74cab8e4@gateway/web/freenode/ip.116.202.184.228] has joined#nitkieee[23:01] he means every vertex has its own linked list that contains vertices it is connected to.[23:01] So in the first graph of that image the adjacency list representation will be[23:02] 1 : 4->NULL

    [23:02] 2: 4->NULL[23:02] == ankitTripathi [75c05593@gateway/web/freenode/ip.117.192.85.147] has joined #nitkieee[23:02] 3: 4-> NULL[23:02] 4: 1->2->3-> NULL[23:03] @abhi-95: can you quickly tell me the advantages of such representation ?[23:04] @abhi ?[23:04] == aadi [73f197e9@gateway/web/freenode/ip.115.241.151.233] has joined #nitkieee[23:05] wats happening??[23:05] @abraham : can u tell me the advantages ?[23:05] The discussion is about Adjacency List

    [23:06] less memory required[23:06] The current question is advantages of List over Matrix[23:06] kk[23:07] matrix uses o(n*n) hence has fast look up[23:07] lesser memory?[23:07] == shravya [dfb590f4@gateway/web/freenode/ip.223.181.144.244] has joined#nitkieee[23:07] @gunjan: can you tell me the memory analysis of both representation ?[23:07] I mean Asymptotic analysis of both representation ![23:08] matrix O(n^2)[23:08] lesser memory : true! But how much ?[23:08] Assume that the number of edge is graph is say "E" and numb

    er or vertex is "V"[23:08] proportional to no. of edges[23:08] proportional to E[23:09] There is no such thing as proportional ![23:09] I need Omega , Theta or Big Oh notation ![23:09] This is easy ![23:10] First what is the complexity of Matrix ?[23:10] O(n^2)[23:10] O(n^2)[23:10] guys its good to have an intuition anyway if u are plainly sayi

  • 8/12/2019 Hangout #6

    3/7

    ng in terms of being proportional.[23:10] O(n*n)[23:10] but ashish is training u already for tech terminology. so its agood thing for you guys.[23:11] == karthik [ab4eae63@gateway/web/freenode/ip.171.78.174.99] has joined #nitkieee[23:12] During a telephonic interview with IBM Linux Development team, I was beaten a lot of times over tech-terminology ![23:12] lesser memory for lists since they just have info of edges present[23:12] Proportional is similar to kind of Theta ! Not for Big Oh or Omega ![23:12] O(V^2) matrix[23:13] O(V+E) list[23:13] @aadi: Exactly ! I hope, you came up on your own without googling :P[23:14] i was read a doc[23:14] reading[23:14] @ankitTripathi : Can you tell me the maximum number of edges in a graph ?[23:14] == abhishek_d [dfee3a1b@gateway/web/freenode/ip.223.238.58.27] has joined #nitkieee[23:15] Guys if u dont know atleast reply :P[23:16] @surya176 : can u tell me ?

    [23:16] v* (v-1) ?[23:17] @surya : yeah somewhat. Its v(v-1) / 2[23:17] by 2 ??[23:17] Yeah ![23:17] VC2?[23:17] selecting any two vertices[23:17] verticesC2[23:17] So basically its nC2[23:17] the direction doesnt matter?[23:18] i guess surya was too particular. you're right as well[23:18] @surya : yeah if direction is there then it will be v(v-1)[23:18] @surya : good work (Y)[23:18] yeah

    [23:18] so it is O(V^2)[23:19] so List = O(V+E) = O(V + V^2) = O(V^2)[23:20] So technically worst case for List and Matrix is same ![23:21] However, in most programming competition or even in practical life E

  • 8/12/2019 Hangout #6

    4/7

    [23:25] Su usually we declare a structure say Edge { int start; intend; }. We make an array of this structure[23:25] am i connected?[23:25] Or u can use STL pair[23:25] Yeah u r connected ![23:25] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has quit [Ping timeout: 246 seconds][23:26] Can someone tell me space complexity of Edge List ![23:27] Theta(E)[23:27] It's trivial. Theta (E) ![23:27] Sorry to interrupt.. but the last thing i read was in practiseE is almost close to V[23:27] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has joined #nitkieee[23:27] Yeah E is close to V and so, the worst case is rare[23:28] We started with Edge List[23:28] we declare a structure say Edge { int start; int end; }. Wemake an array of this structure.[23:28] Simple list of all edges ![23:28] Again Even this has space complexity of O(V^2)[23:29] Now comes the intuition question ! All the 3 methods have space complexity of v^2, but which one is better ?[23:31] matrix gives faster access to check if an edge is there[23:31] Considering only memory = Edge List better than Adjacency L

    ist better than Adjacency Matrix[23:31] @vadi : Yup ![23:31] I think it depends on the input size and requirement specified in the question.Intuition--> Edge list[23:31] List gives faster access to adjacent (or neighbour) vertices[23:32] Edge List gives faster access to typical edges say - Shortest Edge in Graph or Longest , etc[23:33] However typically, List is most widely used followed by Matrix and last is Edge List ![23:34] The only use of Edge List that I know is in Minimum Spanning Tree where you have to sort the edges according to their cost or weight ! (A bit to hi-fi ! Save it for later).

    [23:35] So we will now discuss most imp graph algorithm - BFS ![23:35] Stands for breadth first search ![23:36] I already gave links to various resources from where you could have learned more about BFS. I hope you all have looked into it at least once[23:36] @chinmay : Please keep making copies of this chat at frequent intervals ![23:37] Alright.[23:38] http://www.cosc.canterbury.ac.nz/tad.takaoka/alg/graphalg/bfs.gif[23:38] Here is a nice image I found[23:39] Recall Queue ![23:39] We are going to use here !

    [23:39] @both Ankit : What kind of Data Structure is Queue - LIFO or FIFO ?[23:40] fifo[23:40] FIFO[23:40] (y)[23:40] fifo[23:40] So we choose a source vertex[23:40] say src[23:40] http://commons.wikimedia.org/wiki/File:Animated_BFS.gif[23:40] this is an intuitive animation.

  • 8/12/2019 Hangout #6

    5/7

    [23:41] yeah continue ashish[23:41] Yeah That is a better image[23:41] In this image vertex labelled as "a" is the source ![23:42] == abhi_95 [3d036b78@gateway/web/freenode/ip.61.3.107.120] has quit [Quit: Page closed][23:43] We maintain an array say Arr[V] to store 0 or 1. ) indicating that the vertex has been visited and 1 indicating that it has not been visited ![23:43] == karthik [ab4eae63@gateway/web/freenode/ip.171.78.174.99] has quit [Quit: Page closed][23:43] So the Grey color that you see in animated image is actually visited ![23:44] Initially Q contains "a" and Visted[a] = true;[23:45] Now we iterate until the queue is empty:[23:45] == karthik [75e53ad8@gateway/web/freenode/ip.117.229.58.216] has joined#nitkieee[23:47] We pop the queue. Say x = Q.pop(); if(Visited[x]==false) {Add all its neighbors to the Q; Visited[x] = false; }[23:47] visited[x] =true it should be in the bracket I think[23:47] visited[x]=true right?[23:49] No it will be false !. Let us simplify a bit! Let Visited[]take three values. 0=White, 1 = grey, 2 = black ![23:50] So our condition will be if(Visited==grey) {Add all the white neighbours; Visited = Black; }

    [23:50] == karthik [75e53ad8@gateway/web/freenode/ip.117.229.58.216] has quit [Quit: Page closed][23:51] White means vertex is yet to be visited. Grey means vertexis in the queue. and Black means the vertex was in the queue.[23:53] So this algorithm essentially visits all the vertices in agraph ![23:53] Initially a is added to Q and marked as grey.[23:54] Then "a" is poped. A is grey so we add "b" and "c" (both are white) to the Q. Now we mark a as black[23:54] Our Queue is = b,c[23:54] Now we pop again since queue is non empty[23:55] so we get "b" Again "b" is grey. So d and c are added to Qand marked Grey b is marked black

    [23:56] Q become = c,d,c[23:56] sorry c,d,e[23:57] Now we pop c. Again Grey. We add f and g to Q and mark themgrey. c is marked Black[23:57] Is there a need for grey? I mean the addition of the vertexto the queue serves the purpose. We are not checking if it is grey as such right?[23:58] I encourage you to keep the grey state for now. It can be eliminated but you have to be careful.[23:58] Oh ok.[23:58] @Chinmay : remember you have to check whether the element is in the queue or not ![23:59] Checking can take O(n) where n is number of element in Q. H

    owever, with Grey state this can be done is O(1)[00:00] Yes (y)[00:00] With practice you all will learn to use Q without grey ![00:00] So we keep following the algo until only "h" is left[00:01] We pop "h". We see it is grey. But it has no white neighbors left. So nothing is added. Q is empty. Hence algo terminated ![00:02] == mb1994 [[email protected]] has left #nitkieee ["Leaving"][00:03] So is it clear ?[00:03] yes[00:03] yup

  • 8/12/2019 Hangout #6

    6/7

    [00:03] yeah[00:03] yes[00:03] yes[00:04] yes[00:04] yup[00:04] DFS is exactly same[00:04] We just use a stack instead of Q[00:05] In DFS when we pop we get the last inserted element[00:05] initially we have "a" in stack and a is grey.[00:06] We pop "a" and add b,c to stack[00:06] stack become b,c[00:07] so we pop again. We get "b" . b has 3 neighbors i.e., "a","d" and "e". "a" is black so discarded. "d" and "e" are added to stack. b is marked black[00:08] stack become d,e,c[00:08] We pop again. We get "d" instead of "c"[00:09] d has no white neighbor so nothing is added[00:09] then we get e. E has H as white neighbor which is added[00:10] stack becomes c,h[00:10] It goes on...like this[00:10] http://www.cse.unsw.edu.au/~billw/Justsearch1.gif[00:10] this image sums up the difference ![00:10] Everyone got this ?[00:11] yup

    [00:11] Yes.[00:11] yes[00:11] yes[00:11] yeah[00:11] Codes of BFS and DFS should be at your fingertips ![00:12] Your fingers should be able to type them automatically while you think other stuff ![00:12] kk[00:12] 3 questions on BFS / DFS will be there in ICPC[00:13] Plus BFS / DFS is used in many other algos also[00:15] .[00:15] So we end here[00:16] Not much time is left

    [00:16] and we are way behind schedule[00:17] This is the only time you have. keep practicing[00:17] kk[00:18] Some of you are really working hard :) Appreciate it ! If there is any particular problem that you are stuck on, please ask me ![00:19] Bye[00:19] Good Night[00:19] == gunjan [02331a54@gateway/web/freenode/ip.2.51.26.84] has quit [Quit:Page closed][00:19] bye[00:19] Good night[00:19] gn[00:19] bye thanks

    [00:19] gn[00:19] gn thanks[00:19] good night[00:19] bye thank u[00:19] gn thanks[00:19] == vadi [6ad8969c@gateway/web/freenode/ip.106.216.150.156] has quit [Quit: Page closed][00:19] thank you![00:19] == abraham_ [[email protected]] has quit [Quit: Leaving][00:19] gn

  • 8/12/2019 Hangout #6

    7/7

    [00:19] == ankitTripathi [75c05593@gateway/web/freenode/ip.117.192.85.147] has left #nitkieee [][00:19] == ankit_d [0e601fd8@gateway/web/freenode/ip.14.96.31.216] has quit [Quit: Page closed][00:19] thanks![00:19] gn thanks[00:19] == chinmay_dd [[email protected]] has quit [Quit: Leaving][00:20] == kavya_ [0e60611d@gateway/web/freenode/ip.14.96.97.29] has quit [Quit:Page closed][00:20] == pvk [6ad898b9@gateway/web/freenode/ip.106.216.152.185] has quit [Quit: Page closed][00:20] == abhishek_d [dfee3a1b@gateway/web/freenode/ip.223.238.58.27] has left#nitkieee [][00:21] == aadi [73f197e9@gateway/web/freenode/ip.115.241.151.233] has quit [Quit: Page closed][00:22] == varsha [74cab8e4@gateway/web/freenode/ip.116.202.184.228] has left #nitkieee [][00:24] == shravya [dfb590f4@gateway/web/freenode/ip.223.181.144.244] has left #nitkieee [][00:28] == medhz [7aac8347@gateway/web/freenode/ip.122.172.131.71] has quit [Quit: Page closed]