Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <unordered_map>
  4. #include <unordered_set>
  5. #include <vector>
  6.  
  7. class Graph
  8. {
  9. public:
  10. // Добавить вершину
  11. void AddEdge( int v1, int v2 )
  12. {
  13. m_adjMap[ v1 ].insert( v2 );
  14. m_adjMap[ v2 ].insert( v1 );
  15. }
  16. // Получить вектор всех смежные верщин
  17. std::vector< int > GetNeighbors( int vertex )
  18. {
  19. return std::vector< int >( m_adjMap[ vertex ].begin( ),
  20. m_adjMap[ vertex ].end( ) );
  21. }
  22. // Получить вектор всех вершин
  23. std::vector< int > GetVertices( ) const
  24. {
  25. std::vector< int > vertices;
  26. for( auto pair : m_adjMap )
  27. {
  28. vertices.push_back( pair.first );
  29. }
  30. return vertices;
  31. }
  32. // Проверить, еслть ли ребро
  33. bool HasEdge( int v1, int v2 ) // should be const
  34. {
  35. return ( m_adjMap[ v1 ].count( v2 ) == 1 );
  36. }
  37. // Получить степень вершины
  38. int GetDegree( int vertex )
  39. {
  40. int degree = 0;
  41. for( auto neighbor : m_adjMap[ vertex ] )
  42. {
  43. ++degree;
  44. if( neighbor == vertex )
  45. {
  46. ++degree;
  47. }
  48. }
  49. return degree;
  50. }
  51. // Получить кол-во вершин
  52. int GetSize( )
  53. {
  54. return m_adjMap.size( );
  55. }
  56.  
  57. private:
  58. std::unordered_map< int, std::unordered_set< int > > m_adjMap;
  59. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement