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