Graph Structure Flashcards
Bipartite Graph
Vertices can be divided into 2 sets A and B such that every edge has connection between A and B. And no edge connects the vertices of the same set
Use color code to differentiate. Use opposite color to the neighbours
E = O(V^2)
Bipartite Graph - no cycles
Bipartite graphs do not contain odd length cycles.
If A->B-C forms a triangle then we can’t put B-C in other set as there shouldn’t be any connection between two vertices in one set
Cycles
Starts and ends with same vertex
Acyclic
no cycles
Dense/Sparse tree
Dense: Number of edges close to Max.
Sparse: Number of edges close to Min
Tree
E = V-1 edges.
Acyclic
Sparse tree. As max of V-1 edges. Has unique path between any of part of vertices
Simple Graph
No self-loop edges
No multiple parallel edges
Num of edges : 0 to V^2
Adjacent Vertices
Undirected:If edge exists between two vertices
Directed: 0 ->1 0 is adjacent to 1, but not otherwise
Adjacent Edges
If the two edges have common node
(0,2) (2,4) 2 is common
Degree of Vertex
Undirected: Number of connections
Directed: in-degree/out-degress: num of in / out connections
Connected Graph
If there is a path between every pair of vertices. Meaning we should be able to reach any other node in a Graph.
connected components
If graph has disjoint subgraphs and each subgraph is a component. See if deleting any edge forms connected components and that edge is called cut/bridge for vertex/edge
Adjacency Matrix (AM)
2D array - 1 has connection 0 has no connection
Space: O(V^2)
Time: O(1) read/write
use for small number of edges
Adjacency List (AL)
Create V number of rows and each row contains list of of connections.
For weighted use PAIR to store vertex and weight
Space: O(V+E)
Add Edge: O(1). just append to the list
Search/Check : O(degree(v))
DFS
Time: O(V+E)
Space: O(V)