Graphs Flashcards
Degree of Vertex
Number of Edges sticking out of undirected graph
Self Loop
Edge starting and ending at same vertex
Multigraph
If there are two edges between 2 vertices or there are self loops.
Complete Graph
If there is an edge between every vertex of the graph
Graph
Graph is represent using G=(V,E) where V is set of all vertices, and E is set of all edges
Sum of degree of all vertices in undirected graph
Twice the number of edges 2|E|
Eulerian Cycle
Cover every edge exactly once and come back to starting position
Eulerian Path
Cover every edge exactly once but don’t come back to starting position
Is every EC is EP?
Yes, but not vice-versa
Connected Graph
If you can start from any vertex and reach any other vertex
Connected Component
Is a piece of original graph within which you can start from any vertex and reach any other vertex. But you cannot grow the connected component by adding any more vertex.
For Eulerian Cycle
Every timer we enter a vertex, we can exit it along some other edge. so the degree of vertex should be even. If the degree of any vertex is odd grapg can not have Eulerian cycle
If the graph is connected and degree of every vertex is even does it have Eulerian Cycle
Yes
If graph has 2 vertices with Odd Degree and rest with even degree
It has Eulerian Path
Vertices with Odd dgree 0, EC or EP?
Eulerian Cycle
Vertices with Odd degree 1, EC or EP?
none
Vertices with Odd degree 2, EC or EP?
Eulerian Path
Vertices with Odd degree 2,4,6 …. EC or EP?
Eulerian Path
Graph Representation
1.Edge List: Simple but not efficient
2. Adjacency List:
3. Adjacency Matrix: n*n matrix
4. Adjacency Map:
Number of edges at max in undirected graph
NC2 = n(n-1)/2 = O(n2) n => O(n square)
Adjacency List
From the hashmap you get a vertex object and the class definition of vertex has an attribute which is a pointer to the adjacency list for the vertex. Vertex and adjacency list fused together called Object & Pointers approach.
This representation is not efficient if same set of vertices participate in different graph like road network, railway network or air route. Its better to save vertex separately
Facebook has millions of people and sparse graph of friendship b/w people.So better use Adjacency List.
Adjacency Matrix
Symmetric Matrix: as its undirected graph. In directed graph it may not be symmetric.
Its good if we have to quickly tell if there is an edfe between u and v (u,v)
Diagonal is zero: No self loops
Sparse Graph
In which |E|«_space;|V|@
Adjacency Map
Adjacency map uses a dictionary(hashmap key value pair) to store the neighbours of vertex v instead of list of neighbours( as in adjacency list)