Advertisement
RonKoP

Bipartite

Aug 7th, 2017
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. DFS(G=(V,E))
  2. // π[u] – Parent of u in the DFS tree
  3. 1 for each vertex u ∈ V {
  4. 2 color[u] ← WHITE
  5. 3 π[u]← NULL
  6. 4 time ← 0}
  7. 5 for each vertex u ∈ V {
  8. 6 if color[u] = WHITE
  9. 7 DFS-VISIT(u)}
  10.  
  11. DFS-Visit(u)
  12. // white vertex u has just been discovered
  13. 1 color[u] ← GRAY
  14. 2 time ← time+1
  15. 3 d[u] ← time
  16. 4 for each v ∈ Adj[u] { // going over all edges {u, v}
  17. 5 if color[v] = WHITE {
  18. 6 π[v] ← u
  19. 7 DFS-VISIT(v) }
  20. 8 else if color[v] = GRAY // there is a cycle in the graph
  21. 9 CheckIfOddCycle (u, v);
  22. 10 color[u] ← BLACK
  23. // change the color of vertex u to black as we finished going over it
  24. 11 f[u] ← time ← time+1
  25.  
  26. CheckIfOddCycle(u, v)
  27. 1 int count ← 1;
  28. 2 vertex p = u;
  29. 3 while (p! = v) {
  30. 4 p ← π[p]
  31. 5 count++ }
  32. 6 if count is an odd number {
  33. 7 S.O.P (“The graph is not bipartite!”);
  34. 8 stop the search, as the result is now concluded!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement