0% found this document useful (0 votes)
6 views3 pages

graph and trees

The document discusses the fundamentals of trees and graphs, highlighting the differences between directed and undirected graphs, as well as concepts like weighted edges, connectivity, and graph traversal. It explains various graph representations, including edge lists and adjacency matrices, and their respective time and space complexities. Additionally, it covers properties of graphs such as acyclic graphs, strongly connected graphs, and the implications of density in graph structures.

Uploaded by

labdhi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

graph and trees

The document discusses the fundamentals of trees and graphs, highlighting the differences between directed and undirected graphs, as well as concepts like weighted edges, connectivity, and graph traversal. It explains various graph representations, including edge lists and adjacency matrices, and their respective time and space complexities. Additionally, it covers properties of graphs such as acyclic graphs, strongly connected graphs, and the implications of density in graph structures.

Uploaded by

labdhi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Trees - for n nodes then n-1 edges

One room
One edge for each parent child relationship
Special type of tree

Graph
Nodes and edges, no rules

A graph G is an ordered pair of a set v of vertices and set E of edges


(Ordered — order matters, first vertices)
G= (V,E)

Ordered pair
(A,B) != (B,A)

Unordered pair
{a,b} = {b,a}

An edge is identified by unique endpoints

Directed Edge (u => v , where u is origin, v is destination; connection is unidirectional )


or Undirected Edge (u-v, connection is 2 way)

A graph with all directed edges is called directed graph (Digraph)

A graph with all undirected edges is called undirected graph

Social Network is undirected graph (2 way, if I am your friend, you are mine)

Web pages having link of another pages is directed graph (A -> B -> C)
Web crawling is graph traversal (traversing all nodes on www)

Weighted vs unweighted

Intercity road network - different roads length between two cities


Edges are weighted when we add weight

Self loop — eg web page - which can link to itself — multi tab in page
Multi edge - if it occurs more than one time

For a given graph which has n vertices, number of edges possible ?


minimum edge - 0,
For a directed graph - 0 <= E <= n(n-1)
For undirected graph - 0 <= E <= (n(n-1)/2)

A graph is called dense , if too many edges are there - almost square to number of
vertices
Sparse - less edges — close to vertices

Walk in a graph — sequence of vertices where each adjacent pair is connected by an


edge
Closed walk - starts and end at same vertex and length is greater than zero

Simple path — no vertices are repeated (and no edges are repeated)

Trail - no edges are repeated

Strongly connected graph - path from any vertex to any other vertex — only for directed
graph,
For undirected we call it connected graph if there is a path from any vertex to any other
vertex (A- B - C- D- E)

Weakly Connected - no direct path

A graph with no cycle is called acyclic graph

A directed acyclic graph is called DAG

If we use lists to store a graph :

Edge List — class Edge {


String startVertex; //or int startVertex
String endVertex; //or int endVertext
Int weight;
}

For a directed graph , it matters - start index, endVertext, for undirected just add one
record for single path.

Space complexity of storing graph - O(|V| + |E|)

Find all nodes adjacent to a given node?


— Linear search , find if start and end node exists
Time complexity - O(number of edges);

Check if given nodes are connected - O(number of edges)

If |V| = n
Then 0 <= |E| <= n(n-1) for directed
0 <= |E| < = (n(n-1)/2);

Storing Edges in 2-d array (Matrix)


0 1 2 3 4 5 6 7
0 1 1 1
1 1
2 1
3 1
4 1
5
6
7
A[I][j] - 1 where edge exists

This is called adjacency matrix

Time node of finding adjacent nodes


Linear search on vertex list to find the index number of vertex
Time complexity - o(n)

For adjaceny matrix.. constant time


Total time complexity - O(v)

Finding if 2 nodes are connected


If indexes are given - O(1)
If names are given - we need to find indexes - O(v) for scanning vertex list

To optimize it, save it in hashset , then searching would be O(1) too

Space Complexity - O(v^2)

For dense, it’s fine, but for sparse , it will waste space

To solve space complexity, we can use adjacency list

You might also like