DSA - Graphs & Graph Algorithms Flashcards
How are GRAPHS formed?
Is formed of a set of vertices/nodes & a set of edges between them
What is an unweighted & undirected Graph?
A Graph that points back & forth from each other and has no cost to traverse
What is a unweighted & directed Graph?
A Graph that has no cost to traverse, but each edge can ONLY travel in 1 direction.
What is an weighted & undirected Graph?
A Graph that has a cost to traverse between nodes, and has bi-directional edges.
(CAN TRAVERSE IN EITHER DIRECTION BETWEEN THE NODES)
What is an weighted & directed Graph?
A Graph that has a cost to traverse, but each edge can ONLY travel in 1 direction.
What algorithms are Graphs used in?
ROUTE FINIDING ALGORITHMS
When is a Graph called SIMPLE?
- Has no self loops (EDGES CONNTED TO ITSELF)
- No more than ONE edge between any pair of nodes
What is a Path?
A sequence of vertices v_1,…,v_n such that v_i & v_i+1 are connected by an edge for all 1<= i <= n-1
What is a Cycle?
Is a non-empty path whose first vertex is the same as its last vertex
When is a Path Simple?
If no Vertex appears Twice
- Except a cycle, where the first & last vertex may be the same
When is a Undirected Graph Connected?
If every pair of vertices has a path connecting them
What are the 2 connections for a Directed Graph?
Weakly Connected & Strongly Connected
When is a Directed Graph Weakly Connected?
If for every 2 vertices A & B there is either a path From A to B OR B to A
When is a Directed Graph Strongly Connected?
If there are paths leading both ways
Difference Between Tree & Graph?
Tree can be viewed as a simple connected graph with no cycles & one node identified as a root
In a Graph, no one node has a greater influence then the rest of the graph unless specified otherwise.
(NO ROOT WHICH THERE IS A UNIQUE PATH TO EACH VERTEX)
|=> Does not make sense to speak of parents & children in a graph
What are Neighbours?
Two Vertices connected by an Edge
What does Adjacent mean?
Two edges with a vertex in common
How can you implement a graph like a BST?
Must have an arbitrary number of pointer for each node in graph, due to having an arbitrary number of connection to a graph
What is an Adjacency Matrix?
2D array / Matrix n*n where each cell G[v][w] contains information about the connection from vertex v to vertex w.
In a Adjacency Matrix for an UNWEIGHTED GRAPHS what does G[v][w] = 1 mean?
There is an edge going from v to w
In a Adjacency Matrix for an UNWEIGHTED GRAPHS what does G[v][w] = 0 mean?
There is no such edge (NO CONNECTION)
In a Adjacency Matrix for an WEIGHTED GRAPHS what does G[v][w] = ∞ mean?
There is no such edge (NO CONNECTION)
∞ = (ANY LARGE NUM U WILL NEVER USE)
In a Adjacency Matrix for an WEIGHTED GRAPHS what does G[v][w] = 0* mean?
Does not have SELF links so Zero
- other values possible depend on application
0 ALSO means I don’t need to go further to get to a node
In a Adjacency Matrix for an WEIGHTED GRAPHS what does G[v][w] = x (ANY VAL) mean?
x (CAN BE ANY VALUE)is just the weight of the edge going from v to w
How do you know a graph is Undirected?
If G[v][w] = G[w][v] for all vertices v & w
What is Adjacency List?
Have an array N of n-many Linked List
(one list for every vertex )
Adjacency List for Unweighted Graph?
N[v] is the list of neighbours of v
(Neighbour if there is an edge between v and another one)
DON’T RECORD WEIGHT
Adjacency List for Weighted Graph?
N[v] is the neighbours of v together with the weight of the edge that connects them with v.
Comparison Between Adjacency Matrix(AM) & Adjacency List (AL)?
Checking if there is an edge v => w :
AM: Reading G[v][w] is O(1)
AL: Checks if w is in the list N[v]
(LINEAR SEARCH NOT THROUGH ALL NODE
ONLY NEIGHBOUR)
(COST DEPENDS ON THE DENSITY OF THE
GRAPH)
Allocated Space:
AM: n arrays of size n = O(n*n) = O(n^2) space
AL: n Linked List storing m edges; Total= O(m+n)
(EACH NODE HAS m NO. OF NEIGHBOURS
EDGES = NEIGHBOURS)
( SAVES MEMORY WHEN USING A LARGE
GRAPH)
Traversing Neighbours:
AM: Traversing all G[v][0],…,G[v][n-1] takes O(n) time
(ITERATE THROUGH ALL THE ELEMENTS OF
ROWS IN MATRIX)
AL: Traversing only the Linked List N[v]
(NOT LOOKING AT NON_EDGES, JUST
ITERATE THROUGH THE NODE)
What does SPARSE mean?
There are relatively few edges
Relatively few edges compared to number of Vertices
m/n is SMALL
If m is O(n) or LESS, Besides O(n^2)
Due to it being sparse, the allocated space of AL is much smaller than AM, Then traversing through Neighbours is faster than AM.
What does DENSE mean?
Has Lots of Edges
What is Shortest Path?
A to B is a path for which the sum of the weights are less than or equal to the sum of weights on another path of A to B.
How does Dijkstra’s Algorithm work?
Maintaining certain data & Updating it as it goes to steadily increase the INFO about shortest path between nodes. Till Finally finds shortest path to destination node