Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <unordered_map>
- #include <unordered_set>
- #include <vector>
- class Graph
- {
- public:
- // Добавить вершину
- void AddEdge( int v1, int v2 )
- {
- m_adjMap[ v1 ].insert( v2 );
- m_adjMap[ v2 ].insert( v1 );
- }
- // Получить вектор всех смежные верщин
- std::vector< int > GetNeighbors( int vertex )
- {
- return std::vector< int >( m_adjMap[ vertex ].begin( ),
- m_adjMap[ vertex ].end( ) );
- }
- // Получить вектор всех вершин
- std::vector< int > GetVertices( ) const
- {
- std::vector< int > vertices;
- for( auto pair : m_adjMap )
- {
- vertices.push_back( pair.first );
- }
- return vertices;
- }
- // Проверить, еслть ли ребро
- bool HasEdge( int v1, int v2 ) // should be const
- {
- return ( m_adjMap[ v1 ].count( v2 ) == 1 );
- }
- // Получить степень вершины
- int GetDegree( int vertex )
- {
- int degree = 0;
- for( auto neighbor : m_adjMap[ vertex ] )
- {
- ++degree;
- if( neighbor == vertex )
- {
- ++degree;
- }
- }
- return degree;
- }
- // Получить кол-во вершин
- int GetSize( )
- {
- return m_adjMap.size( );
- }
- private:
- std::unordered_map< int, std::unordered_set< int > > m_adjMap;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement